How to get Device Id in Flutter
Retrieve the device ID in Flutter using the device_info_plus package. Access unique device information for better user identification. Visit rrtutors.com.
Published March 17, 2020
In this post we are going to learn how to get device id in flutter.
For this we are going to use device_info plugin
Let's Start
Step 1: Create flutter application
Step 2: Add dependencies
Update pubspec.yaml file with required dependencies.
dev_dependencies:
device_info: ^0.4.2+1
|
Add required imports in file
import 'package:device_info/device_info.dart';
Step 3: Create deviceinfo.dart file and add below code
import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart';
class GetDeviceInfo extends StatefulWidget {
@override
_GetDeviceInfoState createState() => _GetDeviceInfoState();
}
class _GetDeviceInfoState extends State<GetDeviceInfo> {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo;
fetchDeviceInfo() async {
androidInfo = await deviceInfo.androidInfo;
setState(() {
});
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Get Device Information Example')),
body: (androidInfo==null)?Center(
child: FlatButton(
color: Colors.pink,
child: Text("Device Info",style: TextStyle(
color: Colors.white,fontSize: 18
),),
onPressed: (){
fetchDeviceInfo();
},
),
):Column(
children: <Widget>[
ListTile(
title: Text('Device Id: ${androidInfo.id}, '),
), ListTile(
title: Text('Manufacturer: ${androidInfo.manufacturer}, '),
),
ListTile(
title: Text('Product: ${androidInfo.product}, '),
),
ListTile(
title: Text('Android Version: ${androidInfo.version.codename}, '),
),
],
),
);
}
}
|
Step 4: Update main dart file
void main() => runApp(MyApp());
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.pink
),
home: GetDeviceInfo(),
);
}
}
|
Step 5: Run application