getDeviceInfo static method

Future<String> getDeviceInfo(
  1. BuildContext context
)

Implementation

static Future<String> getDeviceInfo(BuildContext context) async {
  final Map<String, dynamic> deviceData = {};
  final deviceInfo = DeviceInfoPlugin();
  final PackageInfo packageInfo = await PackageInfo.fromPlatform();

  deviceData['appName'] = packageInfo.appName;
  deviceData['version'] = packageInfo.version;

  deviceData['platform'] = Platform.operatingSystem;
  deviceData['platformVersion'] = Platform.operatingSystemVersion;

  if (context.mounted) {
    final MediaQueryData mediaQuery = MediaQuery.of(context);
    deviceData['screenSize'] = {
      'width': mediaQuery.size.width,
      'height': mediaQuery.size.height,
      'devicePixelRatio': mediaQuery.devicePixelRatio,
      'textScaleFactor': mediaQuery.textScaler.scale(1.0),
      'paddingTop': mediaQuery.padding.top,
      'paddingBottom': mediaQuery.padding.bottom,
      'orientation': mediaQuery.orientation.toString(),
    };
  }

  try {
    if (Platform.isAndroid) {
      final androidInfo = await deviceInfo.androidInfo;
      deviceData['device'] = {
        'brand': androidInfo.brand,
        'manufacturer': androidInfo.manufacturer,
        'model': androidInfo.model,
        'product': androidInfo.product,
        'hardware': androidInfo.hardware,
        'device': androidInfo.device,
        'display': androidInfo.display,
        'bootloader': androidInfo.bootloader,
        'deviceFingerprint': androidInfo.fingerprint,
        'supportedAbis': androidInfo.supportedAbis,
        'version': {
          'baseOS': androidInfo.version.baseOS,
          'sdkInt': androidInfo.version.sdkInt,
          'release': androidInfo.version.release,
          'codename': androidInfo.version.codename,
          'incremental': androidInfo.version.incremental,
        },
        'board': androidInfo.board,
        'host': androidInfo.host,
        'id': androidInfo.id,
        'type': androidInfo.type,
      };
    } else if (Platform.isIOS) {
      final iosInfo = await deviceInfo.iosInfo;
      deviceData['device'] = {
        'name': iosInfo.name,
        'systemName': iosInfo.systemName,
        'systemVersion': iosInfo.systemVersion,
        'model': iosInfo.model,
        'localizedModel': iosInfo.localizedModel,
        'identifierForVendor': iosInfo.identifierForVendor,
        'isPhysicalDevice': iosInfo.isPhysicalDevice,
        'utsname': {
          'sysname': iosInfo.utsname.sysname,
          'nodename': iosInfo.utsname.nodename,
          'release': iosInfo.utsname.release,
          'version': iosInfo.utsname.version,
          'machine': iosInfo.utsname.machine,
        },
      };
    } else if (Platform.isMacOS) {
      final macOsInfo = await deviceInfo.macOsInfo;
      deviceData['device'] = {
        'computerName': macOsInfo.computerName,
        'hostName': macOsInfo.hostName,
        'arch': macOsInfo.arch,
        'model': macOsInfo.model,
        'kernelVersion': macOsInfo.kernelVersion,
        'osRelease': macOsInfo.osRelease,
        'activeCPUs': macOsInfo.activeCPUs,
        'memorySize': macOsInfo.memorySize,
        'cpuFrequency': macOsInfo.cpuFrequency,
      };
    } else if (Platform.isWindows) {
      final windowsInfo = await deviceInfo.windowsInfo;
      deviceData['device'] = {
        'computerName': windowsInfo.computerName,
        'numberOfCores': windowsInfo.numberOfCores,
        'systemMemoryInMegabytes': windowsInfo.systemMemoryInMegabytes,
        'userName': windowsInfo.userName,
        'majorVersion': windowsInfo.majorVersion,
        'minorVersion': windowsInfo.minorVersion,
        'buildNumber': windowsInfo.buildNumber,
        'platformId': windowsInfo.platformId,
        'productType': windowsInfo.productType,
        'productName': windowsInfo.productName,
        'releaseId': windowsInfo.releaseId,
        'deviceId': windowsInfo.deviceId,
      };
    } else if (Platform.isLinux) {
      final linuxInfo = await deviceInfo.linuxInfo;
      deviceData['device'] = {
        'name': linuxInfo.name,
        'version': linuxInfo.version,
        'id': linuxInfo.id,
        'idLike': linuxInfo.idLike,
        'versionCodename': linuxInfo.versionCodename,
        'versionId': linuxInfo.versionId,
        'prettyName': linuxInfo.prettyName,
        'buildId': linuxInfo.buildId,
        'variant': linuxInfo.variant,
        'variantId': linuxInfo.variantId,
      };
    }
  } catch (e) {
    deviceData['error'] = e.toString();
  }

  deviceData['locale'] = Platform.localeName;
  deviceData['numberOfProcessors'] = Platform.numberOfProcessors;

  return base64Encode(utf8.encode(json.encode(deviceData)));
}