Device ID in Flutter: device_info_plus package returns null when requesting androidId

13k Views Asked by At

I'm a new Flutter developer, I'm about to develop a Flutter app which needs to use device id so I decided to use the device_info_plus package to get androidId, but when I request device info in order to get androidId within it, it returns null and also does not even exist in toMap() object. I don't know exactly what happened but all the documentation I went through says that this package could give me the device id.

Here is the function

Future<Object?> getId() async {
    var deviceInfo = DeviceInfoPlugin();
    if (Platform.isIOS) { // import 'dart:io'
      var iosDeviceInfo = await deviceInfo.iosInfo;
      return iosDeviceInfo.identifierForVendor; // unique ID on iOS
    } else if(Platform.isAndroid) {
      var androidDeviceInfo = await deviceInfo.androidInfo;
      print(androidDeviceInfo.androidId);
      return androidDeviceInfo.androidId; // unique ID on Android
    }
    return false;
  }
3

There are 3 best solutions below

3
Katarina Sheremet On BEST ANSWER

androidId functionality was removed from version 4.0.0. You can see it from the Changelog here.

If you'd like to get androidId, you need to downgrade device_info_plus in pubspec.yaml

device_info_plus: 3.2.4

P.S. There is the issue on Github that you can upvote. Probably this functionality will be returned.

4
ajip On

Try this

Future<String> getDeviceId() async {
  var deviceInfo = DeviceInfoPlugin();
  late String deviceId;

  if (Platform.isIOS) {
    var iosDeviceInfo = await deviceInfo.iosInfo;
    deviceId = iosDeviceInfo.identifierForVendor!;
  } else if (Platform.isAndroid) {
    var androidDeviceInfo = await deviceInfo.androidInfo;
    deviceId = androidDeviceInfo.androidId!;
  } else {
    deviceId = 'null';
  }
  return deviceId;
}
0
Frederic Chang On

Last time I was using device_info_plus 4.0.0 to get Android information. It is a nice library to get the information from Android.

pub.dev ->device_info_plus

This line will get all information what you want. deviceData = _readAndroidBuildData(await deviceInfoPlugin.androidInfo);

import 'dart:async';
import 'dart:developer' as developer;
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runZonedGuarded(() {
    runApp(const MyApp());
  }, (dynamic error, dynamic stack) {
    developer.log("Something went wrong!", error: error, stackTrace: stack);
  });
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

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

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

  Future<void> initPlatformState() async {
    var deviceData = <String, dynamic>{};

    try {

        if (Platform.isAndroid) {
          deviceData =
              _readAndroidBuildData(await deviceInfoPlugin.androidInfo);
        }

    } on PlatformException {
      deviceData = <String, dynamic>{
        'Error:': 'Failed to get platform version.'
      };
    }

    if (!mounted) return;

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(
            kIsWeb
                ? 'Web Browser info'
                : Platform.isAndroid
                ? 'Android Device Info'
                : Platform.isIOS
                ? 'iOS Device Info'
                : Platform.isLinux
                ? 'Linux Device Info'
                : Platform.isMacOS
                ? 'MacOS Device Info'
                : Platform.isWindows
                ? 'Windows Device Info'
                : '',
          ),
        ),
        body: ListView(
          children: _deviceData.keys.map(
                (String property) {
              return Row(
                children: <Widget>[
                  Container(
                    padding: const EdgeInsets.all(10.0),
                    child: Text(
                      property,
                      style: const TextStyle(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  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(),
        ),
      ),
    );
  }
}

enter image description here