loadPlatformInfo function
Implementation
Future<DeviceInfo> loadPlatformInfo() async {
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo? android;
IosDeviceInfo? ios;
try {
android = await deviceInfo.androidInfo;
} on MissingPluginException {
// Doesn't exist
}
try {
ios = await deviceInfo.iosInfo;
} on MissingPluginException {
// Doesn't exist
}
String? ip;
GeoPoint? geo;
IpStackResponse? ipInfo;
try {
final baseUrl = rproxy.ipstack();
ipInfo = await IpStack.ofBaseUrl(
ThirdPartyCreds.ipstack.credentials,
baseUrl: baseUrl,
).requester();
ip = ipInfo.ip;
geo = GeoPoint.of(ipInfo.latitude, ipInfo.longitude);
} catch (e) {
ip = null;
}
String? language;
try {
language = (await Devicelocale.preferredLanguages)!
.map((language) => language?.toString())
.whereNotNull()
.firstOrNull ??
ipInfo?.location?.languages?.first.code;
} catch (e) {
language = "en";
}
String? locale;
try {
locale = await Devicelocale.currentLocale;
} catch (e) {
locale = "en-us";
}
if (android != null) {
return DeviceInfo(
geo: geo,
ipAddress: ip,
isSimulator: android.isPhysicalDevice != true,
deviceId: android.androidId,
locale: locale!,
language: language!,
deviceModel: android.device,
deviceBrand: android.brand,
softwareVersion: "${android.version}",
deviceType: "android",
software: android.version.baseOS,
);
} else if (ios != null) {
return DeviceInfo(
ipAddress: ip,
geo: geo,
isSimulator: ios.isPhysicalDevice != true,
deviceId: ios.identifierForVendor,
deviceModel: ios.model,
locale: locale!,
language: language!,
deviceBrand: "Apple",
softwareVersion: "${ios.systemVersion}",
deviceType: "iOS",
software: "iOS",
);
} else {
return DeviceInfo.unknown(
geo: geo,
isSimulator: false,
ipAddress: ip,
locale: locale!,
language: language!,
);
}
}