DEV Community

FlutterCorner
FlutterCorner

Posted on

How to Get DeviceId in Flutter with Example Source Code - fluttercorner.com

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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');
}
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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.

How to Get DeviceId in Flutter with Example Source Code

Top comments (0)