Flutter Device Info | Read Device Info on Android and iOS devices with device_info plugin

Last updated Jan 06, 2021


Different types of Mobile devices are available in the market and we don't know on which device our mobile app is running.

To know about complete device info in flutter we have a library called device_info. with this plugin, we can read complete device info inside our application.

device_info plugin will give complete device information on platform specific.

To work with this library first we need to add the library in pubspec.yaml file.

The current version of the library is 1.0.0

device_info 1.0.0

 

To use this library in our widget class we need to import by adding below line

import 'package:device_info/device_info.dart';

 

Then we need to create an instance for DeviceInfoPlugin, which will return platform-specific information.

DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();

 

Get Android Specific device info

DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.model}');

 

This will print the android device model

 

 Get Android Specific device info

DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();

IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print('Running on ${iosInfo.utsname.machine}');

 

In this example, we are using the FutureBuilder to get the device info from the DeviceInfoPlugin class.

 

FutureBuilder(
  builder: (context,snapshaot){
    
    _deviceData=snapshaot.data;
    
  },
  future:initPlatformState()

 

The snapshot object will contain all device information

 

Complete example

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter/services.dart';
class DeviceINfo extends StatelessWidget{
  static final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  Map _deviceData = {};
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
   return MaterialApp(
     home: Scaffold(
       appBar: AppBar(title: Text("Device Info"),),
       body: FutureBuilder(
         builder: (context,snapshaot){
           if(!snapshaot.hasData)
             {
               return Center(child: Text("Not able to find device info"),);
             }
           _deviceData=snapshaot.data;
           return ListView(
               children: _deviceData.keys.map((String property) {
                 
             return Row(
               children: [
                 Container(
                   padding: const EdgeInsets.all(10.0),
                   child: Text(
                     property,
                     style: const TextStyle(
                       fontWeight: FontWeight.bold,
                       color: Colors.deepOrange
                     ),
                   ),
                 ),
                 Expanded(
                     child: Container(
                       padding: const EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
                       child: Text(
                         '${_deviceData[property]}',
                         maxLines: 10,
                         overflow: TextOverflow.ellipsis,
                       ),
                     )),
               ],
             );
           }).toList()
           );
         },
         future:initPlatformState()
       ),
     ),
   );
  }


  Future> initPlatformState() async {
    Map deviceData;

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



  }

  Map _readAndroidBuildData(AndroidDeviceInfo build) {
    return {
      '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,
      'androidId': build.androidId,
      'systemFeatures': build.systemFeatures,
    };
  }

  Map _readIosDeviceInfo(IosDeviceInfo data) {
    return {
      '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,
    };
  }
  
}

 

Read about User Login & Registration with PHP Rest API

 

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1588 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions