11

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;
  }
lepsch
  • 8,927
  • 5
  • 24
  • 44

3 Answers3

11

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.

  • It's weird how the Changelog says it's removed when it reality they just forced that getter to just... always return `null`. Furthermore, this is not documented. Not a good piece of work there. – venir Jul 27 '22 at 13:05
  • 1
    It's not documented nor deprecated*. Which is even worse – venir Jul 27 '22 at 13:27
  • The change log states that it is removed to avoid Google Play policies violations. – Moaz El-sawaf Aug 08 '22 at 23:19
0

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;
}
ajip
  • 11
  • 3
  • It doesn't work; it returns "Instance of Future." – Deogratius Mabima Jul 05 '22 at 07:17
  • I edited the code. that code will return the device id with String format. @DeogratiusMabima – ajip Jul 06 '22 at 06:56
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – mufazmi Jul 06 '22 at 23:22
  • 1
    Thanks a lot! @ajip for showing support. I'm using Samsung A226B(reals mobile phone plugged in by using USB wire) for test and that codes works but still return null on the device id value – Deogratius Mabima Jul 10 '22 at 04:19
0

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

Frederic Chang
  • 509
  • 1
  • 5
  • 13