device_uuid 0.0.4 device_uuid: ^0.0.4 copied to clipboard
A plugin to get the device's UUID. In Android, the UUID is ANDROID_ID encrypted using SHA-1. In iOS, the UUID is a hash string saved in Keychain.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:device_uuid/device_uuid.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _uuid = 'Unknown';
final _deviceUuidPlugin = DeviceUuid();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String uuid;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
uuid = await _deviceUuidPlugin.getUUID() ?? 'Unknown uuid version';
} on PlatformException {
uuid = 'Failed to get uuid version.';
}
// 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(() {
_uuid = uuid;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Device UUID'),
),
body: Center(
child: Text('UUID: $_uuid'),
),
),
);
}
}