Flutter plugin for getting device id.
Add following dependencies to your pubspec.yaml:
dependencies:
device_id: ^1.0.0 #latest version
Add following import to your code:
import 'package:device_id/device_id.dart';
Then you can use the following code to get the device id:
String _deviceId = 'Unknown';
@override
void initState() {
super.initState();
initDeviceState();
}
Future<void> initDeviceState() async {
String deviceId;
try {
deviceId = await DeviceId.getId ?? 'Unknown device id';
} on PlatformException {
deviceId = 'Failed to get device id.';
}
if (!mounted) return;
setState(() {
_deviceId = deviceId;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Device ID App'),
),
body: Center(
child: Text('Device ID: $_deviceId'),
),
),
);
}