How to get device information with Flutter

Here, i going to show you to get the device information using flutter.

First, you need the device_info package from pub. lets follow through.

Import this package in your pubspec.yaml file:

device_info: 0.3.0 

Create a dart file named get_device_info.dart or call it whatever you want, and paste this code inside:

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;
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Get Device Information Example')),
      body: Column(
        children: <Widget>[
          ListTile(
            title: Text('Manufacturer: ${androidInfo.manufacturer}, '),
          ),
          ListTile(
            title: Text('Product: ${androidInfo.product}, '),
          ),
          ListTile(
            title: Text('Android Version: ${androidInfo.version.codename}, '),
          ),
        ],
      ),
    );
  }
}

In your main.dart file:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Code Snippets',
      theme: new ThemeData(primarySwatch: Colors.red),
      home: new GetDeviceInfo(),
    );
  }
}

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.