当我从 qr_code_scanner 包打开 QRView 小部件时出现平台异常
Posted
技术标签:
【中文标题】当我从 qr_code_scanner 包打开 QRView 小部件时出现平台异常【英文标题】:Platform exception when I open the QRView widget from the qr_code_scanner package 【发布时间】:2021-03-14 08:41:26 【问题描述】:我已经从包中复制了示例代码:https://pub.dev/packages/qr_code_scanner
这是我的二维码扫描仪的样子:
import 'package:flutter/material.dart';
'package:qr_code_scanner/qr_code_scanner.dart';
class QrReader extends StatefulWidget
static final routeName = '/QrReader';
@override
_QrReaderState createState() => _QrReaderState();
class _QrReaderState extends State<QrReader>
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
var qrText = "";
QRViewController controller;
@override
void initState()
super.initState();
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text('Scan the QR code'),
),
body: QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
),
);
void _onQRViewCreated(QRViewController controller)
this.controller = controller;
controller.scannedDataStream.listen((scanData)
setState(()
qrText = scanData;
);
);
@override
void dispose()
controller?.dispose();
super.dispose();
当我通过使用 navigator.push 转到 /QrReader 路由打开此屏幕时,QRView 是黑色的,并且出现错误:
E/flutter (30118): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: net.touchcapture.qr.flutterqr/qrview
E/flutter (30118): at io.flutter.plugin.platform.PlatformViewsController$1.createVirtualDisplayForPlatformView(PlatformViewsController.java:192)
E/flutter (30118): at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.create(PlatformViewsChannel.java:104)
E/flutter (30118): at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:59)
E/flutter (30118): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter (30118): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/flutter (30118): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/flutter (30118): at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (30118): at android.os.MessageQueue.next(MessageQueue.java:335)
E/flutter (30118): at android.os.Looper.loop(Looper.java:183)
E/flutter (30118): at android.app.ActivityThread.main(ActivityThread.java:7656)
E/flutter (30118): at java.lang.reflect.Method.invoke(Native Method)
E/flutter (30118): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/flutter (30118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter (30118): , null, null)
[38;5;244mE/flutter (30118): #0 StandardMethodCodec.decodeEnvelope[39;49m
package:flutter/…/services/message_codecs.dart
[38;5;244mE/flutter (30118): #1 MethodChannel._invokeMethod[39;49m
package:flutter/…/services/platform_channel.dart
E/flutter (30118): <asynchronous suspension>
[38;5;244mE/flutter (30118): #2 MethodChannel.invokeMethod[39;49m
package:flutter/…/services/platform_channel.dart
[38;5;244mE/flutter (30118): #3 TextureAndroidViewController._sendCreateMessage[39;49m
package:flutter/…/services/platform_views.dart
[38;5;244mE/flutter (30118): #4 AndroidViewController.create[39;49m
package:flutter/…/services/platform_views.dart
[38;5;244mE/flutter (30118): #5 TextureAndroidViewController.create[39;49m
package:flutter/…/services/platform_views.dart
[38;5;244mE/flutter (30118): #6 TextureAndroidViewController.setSize[39;49m
package:flutter/…/services/platform_views.dart
[38;5;244mE/flutter (30118): #7 RenderAndroidView._sizePlatformView[39;49m
package:flutter/…/rendering/platform_view.dart
[38;5;244mE/flutter (30118): #8 RenderAndroidView.performResize[39;49m
package:flutter/…/rendering/platform_view.dart
[38;5;244mE/flutter (30118): #9 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #10 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #11 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #12 RenderStack.performLayout[39;49m
package:flutter/…/rendering/stack.dart
[38;5;244mE/flutter (30118): #13 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #14 MultiChildLayoutDelegate.layoutChild[39;49m
package:flutter/…/rendering/custom_layout.dart
[38;5;244mE/flutter (30118): #15 _ScaffoldLayout.performLayout[39;49m
package:flutter/…/material/scaffold.dart
[38;5;244mE/flutter (30118): #16 MultiChildLayoutDelegate._callPerformLayout[39;49m
package:flutter/…/rendering/custom_layout.dart
[38;5;244mE/flutter (30118): #17 RenderCustomMultiChildLayoutBox.performLayout[39;49m
package:flutter/…/rendering/custom_layout.dart
[38;5;244mE/flutter (30118): #18 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #19 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #20 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #21 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #22 _RenderCustomClip.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #23 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #24 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #25 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #26 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #27 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #28 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #29 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #30 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #31 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #32 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #33 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #34 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #35 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #36 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #37 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #38 RenderOffstage.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #39 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #40 RenderProxyBoxMixin.performLayout[39;49m
package:flutter/…/rendering/proxy_box.dart
[38;5;244mE/flutter (30118): #41 RenderObject.layout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #42 _RenderTheatre.performLayout[39;49m
package:flutter/…/widgets/overlay.dart
[38;5;244mE/flutter (30118): #43 RenderObject._layoutWithoutResize[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #44 PipelineOwner.flushLayout[39;49m
package:flutter/…/rendering/object.dart
[38;5;244mE/flutter (30118): #45 RendererBinding.drawFrame[39;49m
package:flutter/…/rendering/binding.dart
[38;5;244mE/flutter (30118): #46 WidgetsBinding.drawFrame[39;49m
package:flutter/…/widgets/binding.dart
[38;5;244mE/flutter (30118): #47 RendererBinding._handlePersistentFrameCallback[39;49m
package:flutter/…/rendering/binding.dart
[38;5;244mE/flutter (30118): #48 SchedulerBinding._invokeFrameCallback[39;49m
package:flutter/…/scheduler/binding.dart
[38;5;244mE/flutter (30118): #49 SchedulerBinding.handleDrawFrame[39;49m
package:flutter/…/scheduler/binding.dart
[38;5;244mE/flutter (30118): #50 SchedulerBinding._handleDrawFrame[39;49m
package:flutter/…/scheduler/binding.dart
[38;5;244mE/flutter (30118): #51 _rootRun (dart:async/zone.dart:1190:13)[39;49m
[38;5;244mE/flutter (30118): #52 _CustomZone.run (dart:async/zone.dart:1093:19)[39;49m
[38;5;244mE/flutter (30118): #53 _CustomZone.runGuarded (dart:async/zone.dart:997:7)[39;49m
[38;5;244mE/flutter (30118): #54 _invoke (dart:ui/hooks.dart:251:10)[39;49m
[38;5;244mE/flutter (30118): #55 _drawFrame (dart:ui/hooks.dart:209:3)[39;49m
我应该怎么做才能让它工作?在一切正常之前,我将我的项目更新到了最新版本的颤振,现在我得到了这个错误。
【问题讨论】:
你使用的是flutter稳定版吗?? 也尝试运行 ->flutter clean
一次并再次构建它
我确实跑得很干净,什么都没有。如何查看我是否使用了稳定版本?
运行flutter doctor
并将flutter版本与flutter.dev/docs/development/tools/sdk/releases进行比较
是的,它实际上是在 1.22.4 稳定版本上。
【参考方案1】:
我遇到了同样的问题,并通过在显示 QR 扫描仪之前请求相机许可来解决它。我使用以下库做到了这一点:https://pub.dev/packages/permission_handler/install
我通过返回相机权限状态并在丢失时请求一次的方法解决了它:
Future<PermissionStatus> _getCameraPermission() async
var status = await Permission.camera.status;
if (!status.isGranted)
final result = await Permission.camera.request();
return result;
else
return status;
然后我在将小部件作为对话框打开之前调用该方法
PermissionStatus status = await _getCameraPermission();
if (status.isGranted)
showDialog(
context: context,
builder: (context) => QrScanDialog(onResult: onResult),
);
【讨论】:
【参考方案2】:假设此代码应该可以正常工作,因为它在包示例中给出,无论如何
你可以回到以前的flutter版本
在 Flutter 安装目录下执行
git checkout v1.2.2 (the version you wanted)
现在运行
flutter doctor
或者您可以使用新命令从一个版本转到另一个版本
flutter downgrade
-> 转到上一个
flutter upgrade
-> 更新到新的
【讨论】:
以上是关于当我从 qr_code_scanner 包打开 QRView 小部件时出现平台异常的主要内容,如果未能解决你的问题,请参考以下文章
当我从 UITableViewSource 单击 RowSelected 时如何打开 UIViewController
当我从 UITableViewController 打开自定义 UIView 时,如何关闭自定义视图并返回表格视图?
当我从 firebase 数据库以各自的角色登录时,我想打开三个不同的活动