Open In App

Flutter – Get the Unique Device ID

Last Updated : 24 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Flutter is a UI toolkit developed and maintained by Google. It was released in May 2017 to the public. It is now one of the most popular cross-platform development frameworks among beginners and experienced application developers. It has a straightforward learning curve and uses the Dart programming language for developing the applications. In this article, we will see how we can get the ID of a device in FlutterID. The Device ID can be used to identify the device uniquely.

Approach

We will use the device_info package to get the Information of the device. After that, we can extract the Device ID from that information. The Device ID is of String data type.

Syntax

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
String deviceID = await deviceInfo.androidInfo.androidId;

Note: Before running this code, ensure that you have installed the device_info package in your Flutter project. You can do so by running the below command from your project root:

flutter pub add device_info

Example Project

Dart




import 'package:flutter/material.dart';
import 'package:device_info/device_info.dart';
  
void main() {
    runApp(const DeviceIDApp());
}
  
class DeviceIDApp extends StatelessWidget {
    const DeviceIDApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
        return MaterialApp(
            debugShowCheckedModeBanner: false,
            theme: ThemeData(primarySwatch: Colors.green),
            home: const DeviceIDScreen(),
        );
    }
}
  
class DeviceIDScreen extends StatefulWidget {
    const DeviceIDScreen({Key? key}) : super(key: key);
  
    @override
    _DeviceIDScreenState createState() => _DeviceIDScreenState();
}
  
class _DeviceIDScreenState extends State<DeviceIDScreen> {
    String _deviceID = 'Loading...';
  
    @override
    void initState() {
        super.initState();
        _getDeviceID();
    }
  
    Future<void> _getDeviceID() async {
        DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
        AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
        setState(() {
            _deviceID = androidInfo.androidId;
        });
    }
  
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
                title: const Text('Device ID App'),
            ),
            body: Center(
                child: Text(
                    _deviceID,
                    style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24),
                ),
            ),
        );
    }
}


Output:

WhatsApp-Image-2023-08-09-at-91213-PM



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads