Flutter device_info未处理的异常:MissingPluginException

时间:2020-08-19 10:23:51

标签: android flutter package

我正在尝试在Flutter应用中访问设备信息。在iOS上可以正常使用,但在Android上则出错。

我已将其添加到依赖项下的pubspec.yaml文件中:

device_info: ^0.4.2+6

这是代码:

var deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) {
  IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
} else {
  AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
}

在调用deviceInfo.androidInfo时引发异常:

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

我尝试运行flutter clean并从设备上卸载该应用,然后再次安装,但仍然无法正常工作。

运行flutter doctor的结果:

Flutter (Channel master, 1.21.0-10.0.pre.115)

我已切换到稳定频道:

Flutter (Channel stable, 1.20.2, ... )

但是错误仍然发生!

有什么建议吗?!

4 个答案:

答案 0 :(得分:0)

您需要判断设备。

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
  AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
} else if (Platform.isIOS) {
  IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
}

答案 1 :(得分:0)

能否请您输入您正在使用的完整源代码?因为我已经针对Android 9测试了该库,所以效果很好。

尝试此源代码,它应该可以工作。

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    print('Running on ${androidInfo.model}'); // e.g. "Moto G (4)"
} else {
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    print('Running on ${iosInfo.utsname.machine}'); // e.g. "iPod7,1"
}

我还应该指出,如果您对任何flutter插件或flutter本身有任何疑问,最好在其存储库中创建一个问题:

https://github.com/flutter/plugins

答案 2 :(得分:0)

尝试下面的示例作为参考。

在此之前,请使用 pubspec.yaml 文件中的 device_info:^ 0.4.0 + 4 软件包。

import 'dart:async';

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:device_info/device_info.dart';

void main() {
  runZoned(() {
    runApp(MyApp());
  }, onError: (dynamic error, dynamic stack) {
    print(error);
    print(stack);
  });
}

class MyApp extends StatefulWidget {
  @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 {
    Map<String, dynamic> deviceData;

    try {
      if (Platform.isAndroid) {
        deviceData = _readAndroidBuildData(await deviceInfoPlugin.androidInfo);
      } else if (Platform.isIOS) {
        deviceData = _readIosDeviceInfo(await deviceInfoPlugin.iosInfo);
      }
    } on PlatformException {
      deviceData = <String, dynamic>{
        'Error:': 'Failed to get platform version.'
      };
    }

    if (!mounted) return;

    setState(() {
      _deviceData = deviceData;
    });
  }

  Map<String, dynamic> _readAndroidBuildData(AndroidDeviceInfo build) {
    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,
    };
  }

  Map<String, dynamic> _readIosDeviceInfo(IosDeviceInfo data) {
    return <String, dynamic>{
      'name': data.name,
      'systemName': data.systemName,
      'systemVersion': data.systemVersion,
      'model': data.model,
      'localizedModel': data.localizedModel,
      'identifierForVendor': data.identifierForVendor,
      'isPhysicalDevice': data.isPhysicalDevice,
      'utsname.sysname:': data.utsname.sysname,
      'utsname.nodename:': data.utsname.nodename,
      'utsname.release:': data.utsname.release,
      'utsname.version:': data.utsname.version,
      'utsname.machine:': data.utsname.machine,
    };
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(
              Platform.isAndroid ? 'Android Device Info' : 'iOS 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(),
        ),
      ),
    );
  }
}

答案 3 :(得分:0)

经过一番挣扎,我发现此问题的原因来自MethodChannel。在MainActivity.java文件中,我定义了一个方法通道:

_methodChannel = new MethodChannel(getFlutterView(), "myChannelName");

getFlutterView()替换为getFlutterEngine().getDartExecutor().getBinaryMessenger()之后,一切正常。

我实际上不知道它们之间的关系,但这就是发生的情况。