mobile_unique_id
A Flutter plugin to get unique id of mobile devices. Supported for Android and iOS platforms.
dependencies:
mobile_unique_id: ^1.0.0
import 'package:mobile_unique_id/mobile_unique_id.dart';
If you encounter any issues, open an issue. Even better, raise a ticket on github for suggestions. Pull request are also welcome.
For help getting started with Flutter, view our online documentation.
For help on editing plugin code, view the documentation.
MIT License
Run this command:
With Flutter:
$ flutter pub add mobile_unique_id
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get
):
dependencies:
mobile_unique_id: ^1.0.1
Alternatively, your editor might support flutter pub get
. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:mobile_unique_id/mobile_unique_id.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:mobile_unique_id/mobile_unique_id.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _uniqueId = 'Unknown';
final _mobileUniqueIdPlugin = MobileUniqueId();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await _mobileUniqueIdPlugin.getUniqueId() ?? 'NA';
} on PlatformException {
platformVersion = 'Failed to get mobile unique id.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_uniqueId = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('My flutter app'),
),
body: Center(
child: Text('Mobile has unique id: $_uniqueId\n'),
),
),
);
}
}
Download details:
Author: itsakhiltiwari