使用相机功能时,Flutter 应用程序崩溃并从启动画面重新启动
Posted
技术标签:
【中文标题】使用相机功能时,Flutter 应用程序崩溃并从启动画面重新启动【英文标题】:Flutter app crashs and restart from splash screen when using camera function 【发布时间】:2020-02-19 12:24:31 【问题描述】:我想点击一张图片并将其发送到服务器。如果我们使用gallery function
,该代码运行良好,但当它被camera function
交换时无法运行。
我可以打开相机,点击图片,然后选择正确或错误。
在此步骤之后应用程序崩溃。在这一步之后,需要将图像发送到裁剪屏幕。
代码如下:
Future getImage() async
// var image = await ImagePicker.pickImage(source: ImageSource.gallery);
var _image = await ImagePicker.pickImage(source: ImageSource.camera);
croppedFile = await ImageCropper.cropImage(
sourcePath: _image.path,
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.ratio3x2,
CropAspectRatioPreset.original,
CropAspectRatioPreset.ratio4x3,
CropAspectRatioPreset.ratio16x9
],
androidUiSettings: AndroidUiSettings(
toolbarTitle: 'Cropper',
toolbarColor: Colors.deepOrange,
toolbarWidgetColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.square,
lockAspectRatio: false),
iosUiSettings: IOSUiSettings(
minimumAspectRatio: 1.0,
)
);
if (_pickInProgress)
return;
_pickInProgress = true;
if (_image != null)
setState(()
_newAvatar = croppedFile;
);
_pickInProgress = false;
在Android Studio
中,当过滤器与关键字flutter
一起使用时,我有以下日志:
2020-02-19 17:51:48.404 10631-10631/in.x.demo D/FlutterActivity: Using the launch theme as normal theme.
2020-02-19 17:51:48.413 10631-10631/in.x.demo D/FlutterActivityAndFragmentDelegate: Setting up FlutterEngine.
2020-02-19 17:51:48.413 10631-10631/in.x.demo D/FlutterActivityAndFragmentDelegate: No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment.
2020-02-19 17:51:48.795 10631-10701/in.x.demo I/flutter: Observatory listening on http://127.0.0.1:38436/ayKKbiBs2ts=/
2020-02-19 17:51:49.031 10631-10631/in.x.demo D/FlutterActivityAndFragmentDelegate: Attaching FlutterEngine to the Activity that owns this Fragment.
2020-02-19 17:51:49.112 10631-10631/in.x.demo D/FlutterView: Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@178fa3d
2020-02-19 17:51:49.142 10631-10631/in.x.demo D/FlutterActivityAndFragmentDelegate: Executing Dart entrypoint: main, and sending initial route: /
2020-02-19 17:51:51.013 10631-10673/in.x.demo E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'contains' was called on null.
Receiver: null
Tried calling: contains("access_token")
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 _SplashScreenState.didChangeDependencies (package:demo/screens/common/splash_screen.dart:58:28)
#2 _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:73:64)
#3 _rootRunUnary (dart:async/zone.dart:1134:38)
#4 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
#5 _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
#6 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
#7 Future._propagateToListeners (dart:async/future_impl.dart:709:32)
#8 Future._propagateToListeners (dart:async/future_impl.dart:607:9)
#9 Future._completeWithValue (dart:async/future_impl.dart:524:5)
#10 Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:554:7)
#11 _rootRun (dart:async/zone.dart:1126:13)
#12 _CustomZone.run (dart:async/zone.dart:1023:19)
#13 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
#14 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
#15 _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
#16 _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
2020-02-19 17:51:51.015 10631-10673/in.x.demo I/flutter: genesis started
2020-02-19 17:51:51.106 10631-10673/in.x.demo I/flutter: device ID: ee44d5897844a106
2020-02-19 17:51:52.078 10631-10673/in.x.demo I/flutter: genesis final completed
2020-02-19 17:51:52.102 10631-10673/in.x.demo I/flutter: genesis api called !
2020-02-19 17:51:52.102 10631-10673/in.x.demo I/flutter: hasToken true
2020-02-19 17:51:53.085 10631-10673/in.x.demo E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null.
Receiver: null
Tried calling: findAncestorStateOfType<NavigatorState>()
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 Navigator.of (package:flutter/src/widgets/navigator.dart:1492:19)
#2 _SplashScreenState.didChangeDependencies.<anonymous closure> (package:demo/screens/common/splash_screen.dart:54:23)
#3 new Future.delayed.<anonymous closure> (dart:async/future.dart:316:39)
#4 _rootRun (dart:async/zone.dart:1122:38)
#5 _CustomZone.run (dart:async/zone.dart:1023:19)
#6 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
#7 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
#8 _rootRun (dart:async/zone.dart:1126:13)
#9 _CustomZone.run (dart:async/zone.dart:1023:19)
#10 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949:23)
#11 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
#12 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
#13 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
#14 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
`
【问题讨论】:
你添加权限了吗? @Naeem yes 添加了权限。 我遇到了确切的问题,但在我的情况下,我必须从 initState() 打开相机,但在尝试了类似的 github 问题后,我尝试使用 WidgetsBinding.instance.addPostFrameCallback(); src: didierboelens.com/2019/04/addpostframecallback 并且无法再次观察到这个问题,如果你解决了这个问题,请告诉我们! @princeoo7 你解决了这个错误吗?我有同样的问题 你找到解决这个问题的方法了吗? 【参考方案1】:-
调用'.contains('access_token')'时你的对象为空
在调用“navigator.of”时,您的上下文可能为空
【讨论】:
以上是关于使用相机功能时,Flutter 应用程序崩溃并从启动画面重新启动的主要内容,如果未能解决你的问题,请参考以下文章
在flutter中从image_picker包中打开相机会导致真实设备上的应用程序崩溃,但在模拟器(android)中运行良好
Flutter iOS 应用程序在从 ipa 文件启动时崩溃,但在从 xCode 运行时工作正常