Coder
Coder

Reputation: 658

How to get Device Id in flutter of both Android and IOS

Is there a way to get the device id for android and ios, because when I do it for android using the device_info plugin, it says this:

MissingPluginException(No implementation found for method getAndroidDeviceInfo on channel plugins.flutter.io/device_info)

Upvotes: 4

Views: 16732

Answers (4)

Deepya
Deepya

Reputation: 107

  1. Add version in pubspec.yaml like below and click get dependencies.
    device_id: ^0.2.0

  2. Add import Statement
    import 'package:device_id/device_id.dart';

  3. and call
    String device_id = await DeviceId.getID;

Upvotes: 1

Dyary
Dyary

Reputation: 790

Turns out you don't actually need a package all the functionality to get Device info is inside a flutter class called flutter.io.

You can simply tap into it with this line of code as an import :

import 'dart:io' show Platform;

then to execute platform specific code you can use :

     if (Platform.iOS)
 {//your code} 
else if (Platform.Android)
{//your other code}

Upvotes: 3

Akram Chauhan
Akram Chauhan

Reputation: 1156

Let me explain you this by device_info plugin.

Step 1 Add Dependency

dependencies:
  flutter:
    sdk: flutter
  device_info: ^0.4.0+1

Step 2 Import your file at beginning

  import 'package:device_info/device_info.dart';

Step 3 Create a async function to return device id.

deviceInfo() async{
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
  return androidInfo.id;
}

Step 4 Create a FutureBuilder() to display device id or perform any action.

FutureBuilder(
  future: deviceInfo(),
  builder: (BuildContext context, AsyncSnapshot snap){
    // do nothing...
    if (snap.hasData){
      //your logic goes here.
    }else {
      return new CircularProgressIndicator();
    }
  },
)

I hope that helps you.

Upvotes: 1

Rahul Mahadik
Rahul Mahadik

Reputation: 12261

device_id plugin works fine now with iPhone also.

Bug fixed by them and also updated to swift 4.1.x

To retrieve the id just import (import 'package:device_id/device_id.dart';) and call

String device_id = await DeviceId.getID;

You can check change-log here

Upvotes: 1

Related Questions