5

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.

Chris Chávez
  • 421
  • 5
  • 10
Maccy
  • 116
  • 1
  • 8

1 Answers1

6

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();
    }
  },
)
Hamed
  • 5,867
  • 4
  • 32
  • 56
Akram Chauhan
  • 1,006
  • 1
  • 12
  • 19
  • It's known to be null sometimes, it's documented as "can change upon factory reset". Use at your own risk, and it can be easily changed on a rooted phone. – Kamlesh Aug 31 '20 at 09:01