Flutter - Get Device ID Examples

This tutorial shows you how to get the device ID using Flutter on Android, iOS, Linux, Windows, macOS, and web.

Sometimes, it's necessary to know which device is used to run an application. For example, if you want to detect that the user logged in using a new device. In Flutter application, getting the device ID value depends on the platform where the application runs. Therefore, you may also need to be able to detect the current platform if your application supports multi-platform. This tutorial explains how to get the device ID on various platforms supported by Flutter which includes Android, iOS, Linux, Windows, macOS, and web.

Dependencies

The device_info_plus package can be very useful for getting the device ID. However, the current version doesn't support getting the device ID (ANDROID_ID) of Android devices (was supported on older versions). If your application needs to run on Android, you need to use another plugin called android_id.

  android_id: ^0.2.0
  device_info_plus: ^8.2.0

Add the dependencies to your pubspec.yaml file and run flutter pub get.

Get Android Device ID

Getting the device ID of an Android device can be done using the android_id package. To use the package, you need to add import 'package:android_id/android_id.dart';. After that, get the instance of AndroidId by calling the constructor. The device ID can be obtained by calling the getId method.

  const androidId = AndroidId();
  String? deviceId = await androidId.getId();

Get iOS Device ID

For platforms other than Android, you can use the device_info_plus package. Add import 'package:device_info_plus/device_info_plus.dart'; on the file where you use the package.

To get the device ID of an iOS device, first create an instance of DeviceInfoPlugin. Then, get the iosInfo property which returns Future<IosDeviceInfo>. The IosDeviceInfo has identifierForVendor property which returns a unique UUID value identifying the current device.

  final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  final iosInfo = await deviceInfo.iosInfo;
  String? deviceId = iosInfo.identifierForVendor;

Get Linux Device ID

For Linux, you need to get the LinuxDeviceInfo object from the linuxInfo property of a DeviceInfoPlugin instance. The LinuxDeviceInfo object has a property named machineId which returns a unique machine ID of the local system.

  final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  final linuxInfo = await deviceInfo.linuxInfo;
  String? deviceId = linuxInfo.machineId;

Get Windows Device ID

For Windows, you need to get the WindowsDeviceInfo object from the windowsInfo property of a DeviceInfoPlugin instance. The WindowsDeviceInfo object has a property named deviceId. It's the value displayed as 'Device ID' in Windows Settings which is stored as HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SQMClient\MachineId in the registry key.

  final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  final windowsInfo = await deviceInfo.windowsInfo;
  String deviceId = windowsInfo.deviceId;

Get macOS Device ID

For macOS, you need to get the MacOsDeviceInfo object from the macOsInfo property of a DeviceInfoPlugin instance. The property of MacOsDeviceInfo that you need to get is systemGUID. It returns the GUID of the device.

  final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  final macOsInfo = await deviceInfo.macOsInfo;
  String? deviceId = macOsInfo.systemGUID;

Get Web Device ID

If the Flutter application runs on the web, you can use the combination of vendor, user agent, and hardware concurrency information as an identifier. You need to get the WebBrowserInfo object from the webBrowserInfo property of a DeviceInfoPlugin instance. Then, use the values of vendor, userAgent, and hardwareConcurrency to build the identifier.

  final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  final webBrowserInfo = await deviceInfo.webBrowserInfo;
  String deviceId = '${webBrowserInfo.vendor ?? '-'} + ${webBrowserInfo.userAgent ?? '-'} + ${webBrowserInfo.hardwareConcurrency.toString()}';

Full Code

Below is a Flutter application that displays the device ID. It detects the platform where it runs to execute the platform-specific code.

  import 'dart:io' show Platform;
  
  import 'package:android_id/android_id.dart';
  import 'package:device_info_plus/device_info_plus.dart';
  import 'package:flutter/foundation.dart' show kIsWeb;
  import 'package:flutter/material.dart';
  
  void main() => runApp(const MyApp());
  
  class MyApp extends StatelessWidget {
  
    const MyApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return const MaterialApp(
        title: 'Woolha.com Flutter Tutorial',
        home: GetDeviceIdExample(),
      );
    }
  }
  
  class GetDeviceIdExample extends StatefulWidget {
  
    const GetDeviceIdExample({Key? key}) : super(key: key);
  
    @override
    State<StatefulWidget> createState() {
      return _GetDeviceIdExampleState();
    }
  }
  
  class _GetDeviceIdExampleState extends State<GetDeviceIdExample> {
  
    String? _deviceId;
  
    @override
    void initState() {
      super.initState();
      _getDeviceId();
    }
  
    void _getDeviceId() async {
      final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      String? deviceId;
  
      if (kIsWeb) {
        final webBrowserInfo = await deviceInfo.webBrowserInfo;
        deviceId = '${webBrowserInfo.vendor ?? '-'} + ${webBrowserInfo.userAgent ?? '-'} + ${webBrowserInfo.hardwareConcurrency.toString()}';
      } else if (Platform.isAndroid) {
        const androidId = AndroidId();
        deviceId = await androidId.getId();
      } else if (Platform.isIOS) {
        final iosInfo = await deviceInfo.iosInfo;
        deviceId = iosInfo.identifierForVendor;
      } else if (Platform.isLinux) {
        final linuxInfo = await deviceInfo.linuxInfo;
        deviceId = linuxInfo.machineId;
      } else if (Platform.isWindows) {
        final windowsInfo = await deviceInfo.windowsInfo;
        deviceId = windowsInfo.deviceId;
      } else if (Platform.isMacOS) {
        final macOsInfo = await deviceInfo.macOsInfo;
        deviceId = macOsInfo.systemGUID;
      }
  
      setState(() {
        _deviceId = deviceId;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Woolha.com Flutter Tutorial'),
          backgroundColor: Colors.teal,
        ),
        body: SizedBox(
          width: double.infinity,
          child: Center(
            child: Text('Device ID: ${_deviceId ?? '-'}'),
          ),
        ),
      );
    }
  }

Summary

Getting the device ID in Flutter for most platforms can be done with the help of device_info_plus package. For Android, you need to use a different package called android_id.

You can also read about: