I am using the device_info plugin to display the app version, OS version and model in Text widgets. I make three functions that each return a string.

//OS version
Future<String> _getVersionInfo() async {
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
  return androidInfo.version.release.toString();
}

//app version
Future<String> _getPackageInfo() async {
  PackageInfo _packageInfo = await PackageInfo.fromPlatform();
  return _packageInfo.version.toString();
}

Future<String> _getDeviceModel() async {
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  if (Platform.isAndroid) {
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    return androidInfo.model.toString();
  } else if (Platform.isIOS) {
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    return iosInfo.model.toString();
  } else {
    return ('Error retreiving device model');
  }
}

Then I pass the function to the text Widget.

Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('App Version: ${_getPackageInfo()}'),
        ],
      ),
      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('Device Model: ${_getDeviceModel()}'),
        ],
      ),
      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('OS Version: ${_getVersionInfo()}'),
        ],
      )

Instead of showing the returned string in the Text widget, it shows 'Instance of 'Future'. What am I doing wrong here?


Solution 1: Luke Greenwood

These are asynchronous functions and need to be awaited. I would suggest using something like a FutureBuilder to render these properties.

FutureBuilder<String>(
            future: _getDeviceModel(),
            builder: (BuildContext context,
                AsyncSnapshot<String> snapshot) {
              if (!snapshot.hasData) {
                return CircularProgressIndicator();
              }

              return Text('Device Model: ${snapshot.data}');
          })