flutter 如何解决我dart文件中的这个错误?- 表达式的计算结果不是函数,因此无法调用,(我正在尝试获取设备信息)

0mkxixxg  于 2023-05-29  发布在  Flutter
关注(0)|答案(1)|浏览(164)

这是我在device_info.dart文件中编写的代码(我正在尝试获取设备信息)。

import 'package:device_info/device_info.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import '../domain/model/model.dart';

Future<DeviceInfo> getDeviceDetails() async {
  String name = "unknown";
  String identifier = "unknown";
  String version = "unknown";
  DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();

  try {
    if (defaultTargetPlatform == TargetPlatform.android) {
      //Android
      AndroidDeviceInfo androidDeviceInfo = await deviceInfoPlugin.androidInfo;
          name = "${androidDeviceInfo.brand} ${androidDeviceInfo.model}";
      identifier = androidDeviceInfo.id;
      version = androidDeviceInfo.version.codename;
    }
    if (defaultTargetPlatform == TargetPlatform.iOS) {
      // iOS
      IosDeviceInfo iosDeviceInfo = await deviceInfoPlugin.iosInfo;
      name = "${iosDeviceInfo.systemName} ${iosDeviceInfo.model}";
      identifier = iosDeviceInfo.identifierForVendor;
      version = iosDeviceInfo.systemVersion;
    }
  } on PlatformException {
// return default data error

    return deviceInfoPlugin(name, identifier, version);
  }
  return deviceInfoPlugin(name, identifier, version);
}

1.上面一行显示错误:表达式的计算结果不为函数,因此无法调用。(www.example.com上的device_info包pub.dev与dart >3不兼容,所以我使用了device_info_plus包)

x7yiwoj4

x7yiwoj41#

变更:

return deviceInfoPlugin(name, identifier, version);

致:

return deviceInfoPlugin;

deviceInfoPlugin变量不是函数。它是DeviceInfoPlugin的示例,并且已经包含了在if语句中定义的nameidentifierversion

相关问题