Skip to content

All topics  /  General

Get Device ID Post Format in Flutter

General ·

Hi friends,

In this post, we will learn how to get device id post format in flutter. i explained simply step by step How to get device id in flutter. Here you will learn Simple How to get a Unique Device id in Flutter?. This tutorial will give you simple example How to get Device Id in Flutter.

I will give you simple Example of how to get device id post format in flutter.

So let's see bellow example:

Step 1 : Create New Flutter Project


Step 2 : import Package

- Run this command  
    $ flutter pub add platform_device_id
- Add This line in pubspec.yaml File
    dependencies:
        platform_device_id: ^1.0.1

Step 3 : Put Code in your Main.dart File

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:platform_device_id/platform_device_id.dart';
void main() {
  runApp(MyApp());
}
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}
class _MyAppState extends State {
  String? _deviceId;
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }
  Future initPlatformState() async {
    String? deviceId;
    try {
      deviceId = await PlatformDeviceId.getDeviceId;
    } on PlatformException {
      deviceId = 'Failed to get deviceId.';
    }
    if (!mounted) return;
    setState(() {
      _deviceId = deviceId;
      print("deviceId->$_deviceId");
    });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Get Your Device ID'),
        ),
        body: Center(
          child: Text('Device ID : $_deviceId'),
        ),
      ),
    );
  }
}

Step 4 : Run Main.dart File

Output

I hope it will help you....