How to detect the current running device with Flutter? - flutter

I am developing a Flutter Application and I want to know the current info of the current device running the app programmatically, how can I do that?

To get the current Device Info:
You can use the device_info_plus package like the following:
import 'package:device_info_plus/device_info_plus.dart';
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.model}'); // e.g. "Moto G (4)"
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print('Running on ${iosInfo.utsname.machine}'); // e.g. "iPod7,1"
WebBrowserInfo webBrowserInfo = await deviceInfo.webBrowserInfo;
print('Running on ${webBrowserInfo.userAgent}'); // e.g. "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"

Related

How to get android device version - Flutter

I have been trying to get the Android device version (For example 11, 12). I have been trying to get only the number
This is what I have done till now
void checkVersion(){
print(Platform.operatingSystemVersion); // it prints "sdk_gphone64_arm64-userdebug 12 S2B2.211203.006 8015633 dev-keys"
// I'm able to fetch the version number by splitting the string
// but the problem is that format of above string will vary by
// operating system, so not suitable for parsing
int platformVersion = int.parse(Platform.operatingSystemVersion.split(' ')[1]); it prints '12'
}
Use the device_info_plus plugin and get Android, iOS, macOS, Linux versions with the following snippet:
Future<String> _getOsVersion() async {
final deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
final info = await deviceInfo.androidInfo;
return info.version.release ?? 'Unknown';
}
if (Platform.isIOS) {
final info = await deviceInfo.iosInfo;
return info.systemVersion ?? 'Unknown';
}
if (Platform.isMacOS) {
final info = await deviceInfo.macOsInfo;
return info.osRelease;
}
if (Platform.isLinux) {
final info = await deviceInfo.linuxInfo;
return info.version ?? 'Unknown';
}
return 'Unknown Version';
}
Try device_info_plus to get any device information you need.
Future<String?> getAndroidVersion() async {
if (Platform.isAndroid) {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
return androidInfo.version.release;
}
throw UnsupportedError("Platform is not Android");
}
You can use device_info_plus package to get the device version:
DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
final androidInfo = await deviceInfoPlugin.androidInfo;
return androidInfo.version.sdkInt;
Or if you don't want to use any external plugin, you can use Platform.operatingSystemVersion. But it'll give you:
"sdk_gphone64_arm64-userdebug 12 S2B2.211203.006 8015633 dev-keys"
So what you did is right. You've to split the string and get the device version:
final systemVerion = Platform.operatingSystemVersion;
int deviceVersion = int.parse(operatingSystemVersion.split(' ')[1]);
print(deviceVersion);
//prints '12'

flutter how to make http post using form data?

I'm trying to make http post and I tried many times to make data-form about under parameters.
But I got a error message -{"IsOK":"false","ResultMessage":"Unexpected character encountered while parsing value: G. Path 'MethodName', line 1, position 13.","ResultCode":null,"EventResultYn":null}-
is there any way to make data-form with parameters in pictures?
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart';
void main() async {
var url = 'https://www.lottecinema.co.kr/LCWS/Ticketing/TicketingData.aspx';
// 1.
/* var dic = {
"MethodName": "GetPlaySequence",
"channelType": "HO",
"osType": "W",
"osVersion":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
"playDate": "2022-06-29",
"cinemaID": "1|0001|1013",
"representationMovieCode": ""
};
var parameters = {"paramList:" : dic};*/
//2.
var map = Map<String, String>();
map["MethodName"] = "GetPlaySequence";
map["channelType"] = "HO";
map["osType"] = "W";
map["osVersion"] =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36";
map["playDate"] = "2022-06-29";
map["cinemaID"] = "1|0001|1013";
map["representationMovieCode"] = "";
var parameters = {"paramList": map};
final response = await http.post(Uri.parse(url), headers: {
// "Content-Type": "text/plain",
// "Content-Type": "Application/json"
}, body: parameters
);
runApp(const MyApp());
And This is cURL.
I expect to response has data like this.
{Items: [{CinemaNameKR: "가산디지털", CinemaNameUS: "Gasan", MovieNameKR: "탑건: 매버릭",…},…], ItemCount: 10}
you must use multipart request
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart';
void main() async {
var url = 'https://www.lottecinema.co.kr/LCWS/Ticketing/TicketingData.aspx';
// 1.
/* var dic = {
"MethodName": "GetPlaySequence",
"channelType": "HO",
"osType": "W",
"osVersion":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
"playDate": "2022-06-29",
"cinemaID": "1|0001|1013",
"representationMovieCode": ""
};
var parameters = {"paramList:" : dic};*/
//2.
var map = Map<String, String>();
map["MethodName"] = "GetPlaySequence";
map["channelType"] = "HO";
map["osType"] = "W";
map["osVersion"] =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36";
map["playDate"] = "2022-06-29";
map["cinemaID"] = "1|0001|1013";
map["representationMovieCode"] = "";
// var parameters = {"paramList": map};
var request = http.MultipartRequest("POST", Uri.parse("$url"));
request.fields.addAll(map);
http.StreamedResponse response = await request.send();
var responseString = await response.stream.bytesToString();
pring("STATUS CODE : ${response.statusCode}");
pring("RESPONSE BODY : ${responseString}");
runApp(const MyApp());

Best way to find device information on flutter app

I'm develop flutter app and I would like to show device model name on screen (see image below)
I found some flutter plugin like https://pub.dev/packages/device_info but it not show exactly model name. Any idea how do i find device model name?
You can use the package device_info_plus
import 'package:device_info_plus/device_info_plus.dart';
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.model}'); // e.g. "Moto G (4)"
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print('Running on ${iosInfo.utsname.machine}'); // e.g. "iPod7,1"
WebBrowserInfo webBrowserInfo = await deviceInfo.webBrowserInfo;
print('Running on ${webBrowserInfo.userAgent}'); // e.g. "Mozilla/5.0

Is there any way i can get the name of the user of the phone(android) in flutter app?

Guys I am trying to get the name of the user who uses the phone. Is there any flutter plugin which can provide me this functionality
Hello you can use this plugin device_info, you can see the documentation about it here :
https://pub.dev/packages/device_info#-example-tab-
import 'package:device_info/device_info.dart';
void getDeviceinfo() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo; // instantiate Android Device Information
IosDeviceInfo iosInfo = await deviceInfo.iosInfo; // instantiate IOS Device Information
print("for Android : ${androidDeviceInfo.product}");
print("for IOS : ${iosInfo.name}");
}

How to get unique device id in flutter?

In Android we have, Settings.Secure.ANDROID_ID. I do not know the iOS equivalent.
Is there a flutter plugin or a way to get a unique device id for both Android and IOS in flutter?
Null safe code
Use device_info_plus plugin developed by Flutter community. This is how you can get IDs on both platform.
In your pubspec.yaml file add this:
dependencies:
device_info_plus: ^3.2.3
Create a method:
Future<String?> _getId() async {
var deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) { // import 'dart:io'
var iosDeviceInfo = await deviceInfo.iosInfo;
return iosDeviceInfo.identifierForVendor; // unique ID on iOS
} else if(Platform.isAndroid) {
var androidDeviceInfo = await deviceInfo.androidInfo;
return androidDeviceInfo.androidId; // unique ID on Android
}
}
Usage:
String? deviceId = await _getId();
There is a plugin called device_info. You can get it here.
Check the official example here
static Future<List<String>> getDeviceDetails() async {
String deviceName;
String deviceVersion;
String identifier;
final DeviceInfoPlugin deviceInfoPlugin = new DeviceInfoPlugin();
try {
if (Platform.isAndroid) {
var build = await deviceInfoPlugin.androidInfo;
deviceName = build.model;
deviceVersion = build.version.toString();
identifier = build.androidId; //UUID for Android
} else if (Platform.isIOS) {
var data = await deviceInfoPlugin.iosInfo;
deviceName = data.name;
deviceVersion = data.systemVersion;
identifier = data.identifierForVendor; //UUID for iOS
}
} on PlatformException {
print('Failed to get platform version');
}
//if (!mounted) return;
return [deviceName, deviceVersion, identifier];
}
You can store this UUID in the Keychain. This way you can set an unique ID for your device.
UPDATE
device_info is now device_info_plus
I just published a plugin to provide a solution to your problem.
It uses Settings.Secure.ANDROID_ID for Android and relies on identifierForVendor and the keychain for iOS to make the behaviour equivalent to Android's.
Here's the link.
Update 1/3/2021: The recommended way is now the extended community plugin called device_info_plus. It supports more platforms than device_info and aims to support all that are supported by flutter. Here is an example usage:
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:device_info_plus/device_info_plus.dart';
import 'dart:io';
Future<String> getDeviceIdentifier() async {
String deviceIdentifier = "unknown";
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
deviceIdentifier = androidInfo.androidId;
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
deviceIdentifier = iosInfo.identifierForVendor;
} else if (kIsWeb) {
// The web doesnt have a device UID, so use a combination fingerprint as an example
WebBrowserInfo webInfo = await deviceInfo.webBrowserInfo;
deviceIdentifier = webInfo.vendor + webInfo.userAgent + webInfo.hardwareConcurrency.toString();
} else if (Platform.isLinux) {
LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
deviceIdentifier = linuxInfo.machineId;
}
return deviceIdentifier;
}
Use device_id plugin
Add in your following code in your .yaml file.
device_id: ^0.1.3
Add import in your class
import 'package:device_id/device_id.dart';
Now get device id from:
String deviceid = await DeviceId.getID;
I release a new flutter plugin client_information might help. It provide a simple way to get some basic device information from your application user.
add to pubspec.yaml
dependencies:
...
client_information: ^1.0.1
import to your project
import 'package:client_information/client_information.dart';
then you can get device ID like this
/// Support on iOS, Android and web project
Future<String> getDeviceId() async {
return (await ClientInformation.fetch()).deviceId;
}
As of 2022, December, last status about getting device id :
device_info_plus don't give unique device id anymore. So I started to use platform_device_id package.
I tested it on Android and it worked as same as device_info previously and provide the same id value. It also has a simple usage :
String deviceId = await PlatformDeviceId.getDeviceId;
This package uses updated android embedding version and also has null safety support.
Latest:
The plugin device_info has given deprecation notice and replaced by
device_info_plus
Example:
dependencies:
device_info_plus: ^2.1.0
How to use:
import 'package:device_info_plus/device_info_plus.dart';
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.model}'); // e.g. "Moto G (4)"
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print('Running on ${iosInfo.utsname.machine}'); // e.g. "iPod7,1"
WebBrowserInfo webBrowserInfo = await deviceInfo.webBrowserInfo;
print('Running on ${webBrowserInfo.userAgent}'); // e.g. "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"
You can check here full example:
For Unique ID:
You can use following code to get Unique ID:
if (kIsWeb) {
WebBrowserInfo webInfo = await deviceInfo.webBrowserInfo;
deviceIdentifier = webInfo.vendor +
webInfo.userAgent +
webInfo.hardwareConcurrency.toString();
} else {
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
deviceIdentifier = androidInfo.androidId;
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
deviceIdentifier = iosInfo.identifierForVendor;
} else if (Platform.isLinux) {
LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
deviceIdentifier = linuxInfo.machineId;
}
}
edit: There is no androidId on since v4.1.0.
androidID is removed since v4.1.0. Check the changelog.
android_id package is recommanded to get the correct androidId.
Add the following code in your .yaml file.
device_info_plus: ^1.0.0
I used the following approach to get the device info that support in all platforms (i.e.) Android, IOS and Web.
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
Future<String> getDeviceIdentifier() async {
String deviceIdentifier = "unknown";
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (kIsWeb) {
WebBrowserInfo webInfo = await deviceInfo.webBrowserInfo;
deviceIdentifier = webInfo.vendor +
webInfo.userAgent +
webInfo.hardwareConcurrency.toString();
} else {
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
deviceIdentifier = androidInfo.androidId;
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
deviceIdentifier = iosInfo.identifierForVendor;
} else if (Platform.isLinux) {
LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
deviceIdentifier = linuxInfo.machineId;
}
}
return deviceIdentifier;
}
Use device_info_plus package developed by Flutter community. This is how you can get IDs on both platform.
In your pubspec.yaml file add this:
dependencies:
device_info_plus: ^3.2.3
Create a method:
Future<String> getUniqueDeviceId() async {
String uniqueDeviceId = '';
var deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) { // import 'dart:io'
var iosDeviceInfo = await deviceInfo.iosInfo;
uniqueDeviceId = '${iosDeviceInfo.name}:${iosDeviceInfo.identifierForVendor}'; // unique ID on iOS
} else if(Platform.isAndroid) {
var androidDeviceInfo = await deviceInfo.androidInfo;
uniqueDeviceId = '${androidDeviceInfo.name}:${androidDeviceInfo.id}' ; // unique ID on Android
}
return uniqueDeviceId;
}
Usage:
String deviceId = await getUniqueDeviceId();
Output:
M2102J20SG::SKQ1.211006.001
Note:
Do not use androidDeviceInfo.androidId. This would change when your mac address changes. Mobile devices above Android OS 10/11 will generate a randomized MAC. This feature is enabled by default unless disabled manually. This would cause the androidId to change when switiching networks. You can confirm this by yourself by changing androidDeviceInfo.id to androidDeviceInfo.androidId above.
you can probably get away with using only androidDeviceInfo.name as it would not change ever.
androidDeviceInfo.id can also change if OS is updated as it is an android os version.
androidDeviceInfo.androidId should only be used if device uses fix mac address as mentioned in point 1. Otherwise, either use *.name only or androidDeviceInfo.id alongside with *.name.
android_id: ^0.1.3+1
Use this package but it only works on android.
device_info_plus: ^8.0.0
Use this package it works on IOS, Android, Web.
If you're serving ads you can use ASIdentifierManager. You should only use it for ads. There is no general UDID mechanism provided by the OS on iOS, for privacy reasons.
If you're using firebase_auth plugin you could signInAnonymously and then use the id of the FirebaseUser. This will give you an identifier that is specific to your Firebase app.