How to Get DeviceId in Flutter ?
Hello Guys How are you all ? hope you all are fine. In some case where you need device id in some APIs call like register, login etc. so how to get deviceId in flutter. Lets start this article without wasting your time.
Install device_id Package and put in dependencies in pubspec.yaml file. As like below.
dependencies:
flutter:
sdk: flutter
device_id: ^0.2.0
Get the dependencies by command pub get.
Now you are ready to use this plugin.
First of all define variable DeviceId in file where you want to use device_id as like below
String deviceID;
Now make method that will return you device_id as string same like below
void getDeviceID() async {
String deviceId = await DeviceId.getID;
deviceID = 'Device ID is $deviceId';
print('Device ID is $deviceId');
}
Now, Trigger this method in initState so you will get device_id when initstate will build.
@override
void initState() {
// TODO: implement initState
super.initState();
getDeviceID();
}
That’s it. Now you can use Device_id any where, also you can add DeviceId in setState so it can use anywhere I am pasting my full source code here.
Top comments (0)