fluttercommunity / android_id

Maintainer: @nohli
https://pub.dev/packages/android_id
BSD 3-Clause "New" or "Revised" License
34 stars 16 forks source link

`androidId` get result "Instance of 'Future<String?>'" #47

Closed blue492 closed 1 year ago

blue492 commented 1 year ago

Hi, when I execute the following code, the variable androidId get this result Instance of 'Future<String?>' Why it happen?

 static Future<String> get_device_Id(BuildContext context) async {
     DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
     if (Theme.of(context).platform == TargetPlatform.iOS) {
       IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
       return iosDeviceInfo.identifierForVendor.toString(); // unique ID on iOS
     } else {
       var _androidIdPlugin = AndroidId();
       return await _androidIdPlugin.getId().toString(); // unique ID on Android
     }
   }
nohli commented 1 year ago

Can I see the full method, please. Is it async?

blue492 commented 1 year ago

Can I see the full method, please. Is it async?

I updated the description

nohli commented 1 year ago

Are you awaiting its result?

final id = await get_device_Id(context)?

If yes, do it like this:

final String? id = await AndroidId().getId(); return id ?? 'null'; // unique ID on Android

blue492 commented 1 year ago

Are you awaiting its result?

final id = await get_device_Id(context)?

If yes, do it like this:

final String? id = await AndroidId().getId(); return id.toString(); // unique ID on Android

Are you awaiting its result? Yes But why? same method worked before

nohli commented 1 year ago

Do you see a difference between

return await _androidIdPlugin.getId().toString()

and

return (await _androidIdPlugin.getId()).toString()

blue492 commented 1 year ago

Do you see a difference between

return await _androidIdPlugin.getId().toString()

and

return (await _androidIdPlugin.getId()).toString()

return (await _androidIdPlugin.getId()).toString() solved the problem, thanks