How to Get Device ID in Flutter - (2023)

In-app development sometimes we need to deal with the device id to put the restriction or identity user as we prevent multiple registrations from the same device. So in this article, I am going to share How to Get a Device ID in Flutter.

 

How to Get Device ID in Android And IOS :

The first step is we need to add the latest version of the device_info_plus package in our project pubspec.yaml.

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  device_info_plus: ^8.1.0

 

The second step is to use the below piece of code for getting the device id.

 

 Future<String> getDeviceId() async {
    String deviceID = "";
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      deviceID = androidInfo.id;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      deviceID = iosInfo.identifierForVendor!;
    }
    return deviceID;
  }

 

You can see the above code we are going to get the device id for Android and IOS. We are using properties of package id for Android and identifierForVendor for IOS.

Note:

In ios every time generate a new device id in debug mode if you uninstall the app and install it again. However, if you prevent this issue run your app in Profile or Release mode.

 

Full Example:

import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';

class Errorgram extends StatefulWidget {
  const Errorgram({Key? key}) : super(key: key);

  @override
  State<Errorgram> createState() => _ErrorgramState();
}

class _ErrorgramState extends State<Errorgram> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        bottom: false,
        child: Scaffold(
          appBar: AppBar(
            title: Text("Errorgram"),
          ),
          body: Center(
            child: FutureBuilder<String>(
                future: getDeviceId(),
                builder: (context, AsyncSnapshot<String> snapshot) {
                  return Text(
                    "Device id - " + snapshot.data.toString(),
                    style: TextStyle(fontSize: 20, color: Colors.red),
                  );
                }),
          ),
        ));
  }

  Future<String> getDeviceId() async {
    String deviceID = "";
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      deviceID = androidInfo.id;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      deviceID = iosInfo.identifierForVendor!;
    }
    return deviceID;
  }
}

 

Output:

 

Conclusion:

In this article, we learned about  How to Get a Device ID in Flutter step by step for Android and IOS. I hope this article may help you.

 

 

Tags: