How to Get Device Id in Flutter of Both Android and Ios

How to get Device Id in flutter of both Android and IOS

device_id plugin works fine now with iPhone also.

Bug fixed by them and also updated to swift 4.1.x

To retrieve the id just import (import 'package:device_id/device_id.dart';) and call

String device_id = await DeviceId.getID;

You can check change-log here

Flutter - Get Unique Device ID

Use device_info_plus plugin.

In your pubspec.yaml file add this

dependencies:
device_info_plus: any

Create a method:

import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
// ...

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 {
var androidDeviceInfo = await deviceInfo.androidInfo;
return androidDeviceInfo.androidId; // Unique ID on Android
}
}

Use it like:

String? deviceId = await _getId();

or

_getId().then((id) {
String? deviceId = id;
});

What can i use as a device identifier in flutter?

Apparently device_id plugin have some bugs with newest versions of flutter. You can use device_info instead.
Here is an example:

 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];
}

How to get device id in flutter

Let me explain to you this by device_info plugin.

Step 1
Add Dependency

dependencies:
flutter:
sdk: flutter
device_info: ^0.4.0+1

Step 2
Import your file at the beginning

  import 'package:device_info/device_info.dart';

Step 3
Create an async function to return device id.

deviceInfo() async{
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
return androidInfo.id;
}

Step 4
Create a FutureBuilder() to display device id or perform any action.

FutureBuilder(
future: deviceInfo(),
builder: (BuildContext context, AsyncSnapshot snap){
// do nothing...
if (snap.hasData){
//your logic goes here.
}else {
return new CircularProgressIndicator();
}
},
)

Device ID in Flutter: device_info_plus package returns null when requesting androidId

androidId functionality was removed from version 4.0.0.
You can see it from the Changelog here.

If you'd like to get androidId, you need to downgrade device_info_plus in pubspec.yaml

device_info_plus: 3.2.4

P.S. There is the issue on Github that you can upvote. Probably this functionality will be returned.

Get AdMob test device ids for both iOS and Android in Flutter project

After a while of looking through logs, I realized there are some missing steps in Flutter's package explanation.

First, Firebase needs to be linked to you AdMob app, AND you need to re-download the GoogleService-Info.plist file for iOS and the google-services.json for Android.

After that, in iOS you need to go into Settings -> Privacy -> Advertising and disable Limit Ad Tracking. This will allow for the ID to be printed.



Related Topics



Leave a reply



Submit