Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get device id in flutter

Tags:

flutter

I want to use device ID and pass to a FutureBuilder() to perform some action. Can anyone explain or provide some code to illustrate that?

I've tried using device_id flutter plugin but no luck.

I've checked with device_info but not sure how to use it properly with FutureBuilder.

like image 927
Maccy Avatar asked May 03 '19 05:05

Maccy


People also ask

What is the ID of a device?

A device ID is a unique, anonymized string of numbers and letters that identifies every individual smartphone or tablet in the world. It is stored on the mobile device and can be retrieved by any app that is downloaded and installed.

How do I get a new device ID?

You'll have to fully format your device data to change your Android phone's device ID. As the device ID is generated when you first set up the device, resetting the phone will change the Android device ID automatically.


1 Answers

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();
    }
  },
)
like image 110
Akram Chauhan Avatar answered Oct 05 '22 23:10

Akram Chauhan