Last active
          December 30, 2023 05:40 
        
      - 
      
 - 
        
Save corbindavenport/d04085e2ac42da303efbaccaa717f223 to your computer and use it in GitHub Desktop.  
    Get CPU architecture in Dart
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import 'dart:io' as io; | |
| // Function get current CPU architecture | |
| Future<String> getCPUArchitecture() async { | |
| if (io.Platform.isWindows) { | |
| var cpu = envVars['PROCESSOR_ARCHITECTURE']; | |
| return cpu; | |
| } else { | |
| var info = await io.Process.run('uname', ['-m']); | |
| var cpu = info.stdout.toString().replaceAll('\n', ''); | |
| return cpu; | |
| } | |
| } | |
| var cpu = await getCPUArchitecture(); | |
| print(cpu); // Example output: amd64 | 
I modify it for better use :)
import 'dart:io';
/// Copy & modify from GitHub Gists
/// https://gist.github.com/corbindavenport/d04085e2ac42da303efbaccaa717f223
class CPUArch {
  // Function get current CPU architecture
  static Future<String> getCPUArchitecture() async {
    var cpu;
    if (Platform.isWindows) {
      cpu = Platform.environment['PROCESSOR_ARCHITECTURE'];
      // var cpu = envVars['PROCESSOR_ARCHITECTURE'];
    } else {
      var info = await Process.run('uname', ['-m']);
      cpu = info.stdout.toString().replaceAll('\n', '');
    }
    switch (cpu) {
      case 'x86_64' || 'X86_64' || 'x64' || 'X64' || 'AMD64':
        cpu = 'amd64';
        break;
      case 'x86' || 'X86' || 'i386' || 'I386' || 'x32' || 'X32' || '386' || 'AMD32':
        cpu = 'amd32';
        break;
    }
    return cpu;
  }
}/// By giving a demo
CPUArch.getCPUArchitecture();
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
thx!