Flutter Device Info

Flutter Device Info:

This Flutter device Info plugin used to get the current device information in Flutter App.

 

Using Device Info In Flutter:

Adding Device Info Package into Flutter:

To use this Device Info Functionality we have to add its dependency package into the pubspec.yaml file:

use below code to add dependency package in pubspec.yaml file.

 

dependencies:

device_info: ^0.2.1

 

Install Device Info dependency package from terminal Editor:

You can install packages from the command line with Flutter by using below command:

$ flutter packages get

Bydefault, your Flutter terminal editor might support flutter packages get.

Importing Device Info  in flutter:

To use Device Info  in Flutter Dart code we have to import into the dart code for coding. without importing Device Info  if we use it in coding then it shows error or exception that is package not found error.

To import dependency package into dart code use below code.

import 'package:device_info/device_info.dart';

 

Below is the sample  Example code of  Flutter Device Info :

 

import 'package:device_info/device_info.dart';

DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.model}'); // e.g. "Moto G (4)"

IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print('Running on ${iosInfo.utsname.machine}'); // e.g. "iPod7,1"

 

Below is the Complete Example of  Flutter Device Info :

Create a new Flutter project and  then copy and paste the below code into example/lib/main.dart file:

 

 

import 'dart:async';

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:device_info/device_info.dart';

void main() {
runZoned(() {
runApp(new MyApp());
}, onError: (dynamic error, dynamic stack) {
print(error);
print(stack);
});
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
static final DeviceInfoPlugin deviceInfoPlugin = new DeviceInfoPlugin();
Map<String, dynamic> _deviceData = <String, dynamic>{};

@override
void initState() {
super.initState();
initPlatformState();
}

Future<Null> initPlatformState() async {
Map<String, dynamic> deviceData;

try {
if (Platform.isAndroid) {
deviceData = _readAndroidBuildData(await deviceInfoPlugin.androidInfo);
} else if (Platform.isIOS) {
deviceData = _readIosDeviceInfo(await deviceInfoPlugin.iosInfo);
}
} on PlatformException {
deviceData = <String, dynamic>{
'Error:': 'Failed to get platform version.'
};
}

if (!mounted) return;

setState(() {
_deviceData = deviceData;
});
}

Map<String, dynamic> _readAndroidBuildData(AndroidDeviceInfo build) {
return <String, dynamic>{

'version.securityPatch': build.version.securityPatch,
'version.sdkInt': build.version.sdkInt,
'version.release': build.version.release,
'version.previewSdkInt': build.version.previewSdkInt,
'version.incremental': build.version.incremental,
'version.codename': build.version.codename,
'version.baseOS': build.version.baseOS,
'board': build.board,
'bootloader': build.bootloader,
'brand': build.brand,
'device': build.device,
'display': build.display,
'fingerprint': build.fingerprint,
'hardware': build.hardware,
'host': build.host,
'id': build.id,
'manufacturer': build.manufacturer,
'model': build.model,
'product': build.product,
'supported32BitAbis': build.supported32BitAbis,
'supported64BitAbis': build.supported64BitAbis,
'supportedAbis': build.supportedAbis,
'tags': build.tags,
'type': build.type,
'isPhysicalDevice': build.isPhysicalDevice,
};
}

Map<String, dynamic> _readIosDeviceInfo(IosDeviceInfo data) {
return <String, dynamic>{
'name': data.name,
'systemName': data.systemName,
'systemVersion': data.systemVersion,
'model': data.model,
'localizedModel': data.localizedModel,
'identifierForVendor': data.identifierForVendor,
'isPhysicalDevice': data.isPhysicalDevice,
'utsname.sysname:': data.utsname.sysname,
'utsname.nodename:': data.utsname.nodename,
'utsname.release:': data.utsname.release,
'utsname.version:': data.utsname.version,
'utsname.machine:': data.utsname.machine,
};
}

@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text(
Platform.isAndroid ? 'Android Device Info' : 'iOS Device Info'),
),
body: new ListView(
shrinkWrap: true,
children: _deviceData.keys.map((String property) {
return new Row(
children: <Widget>[
new Container(
padding: const EdgeInsets.all(10.0),
child: new Text(
property,
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
),
new Expanded(
child: new Container(
padding: const EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
child: new Text(
'${_deviceData[property]}',
overflow: TextOverflow.ellipsis,
),
)),
],
);
}).toList(),
),
),
);
}
}

Leave a Reply

Categories

Shein WW