ImagePicker.getImage()にて実装したカメラ機能で、カメラ撮影後「OK」を押下すると
別の画面遷移としての処理である、Navigator.pushでこけて失敗します。
以下、エラーメッセージです。
I/Timeline( 6877): Timeline: Activity_idle id: android.os.BinderProxy@ede736a time:6959504 E/flutter ( 6877): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null. E/flutter ( 6877): Receiver: null E/flutter ( 6877): Tried calling: findAncestorStateOfType<NavigatorState>() E/flutter ( 6877): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) E/flutter ( 6877): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:2136:19) E/flutter ( 6877): #2 Navigator.push (package:flutter/src/widgets/navigator.dart:1758:22) E/flutter ( 6877): #3 CameraState.transitionTo (package:flutter_gowjp/camera.dart:127:15) E/flutter ( 6877): #4 CameraState._takePicture.<anonymous closure> (package:flutter_gowjp/camera.dart:95:9) E/flutter ( 6877): #5 State.setState (package:flutter/src/widgets/framework.dart:1236:30) E/flutter ( 6877): #6 CameraState._takePicture (package:flutter_gowjp/camera.dart:93:7) E/flutter ( 6877): <asynchronous suspension> E/flutter ( 6877): #7 CameraState.initState (package:flutter_gowjp/camera.dart:110:5) E/flutter ( 6877): #8 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4649:58) E/flutter ( 6877): #9 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4485:5) E/flutter ( 6877): #10 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14) E/flutter ( 6877): #11 Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18) E/flutter ( 6877): #12 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5839:14) E/flutter ( 6877): #13 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14) E/flutter ( 6877): #14 Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18) E/flutter ( 6877): #15 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4536:16) E/flutter ( 6877): #16 Element.rebuild (package:flutter/src/widgets/framework.dart:4227:5) E/flutter ( 6877): #17 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4490:5) E/flutter ( 6877): #18 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4485:5) E/flutter ( 6877): #19 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14) E/flutter ( 6877): #20 Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18) E/flutter ( 6877): #21 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5839:14) E/flutter ( 6877): #22 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14) E/flutter ( 6877): #23 Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18) E/flutter ( 6877): #24 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5839:14) E/flutter ( 6877): #25 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14) E/flutter ( 6877): #26 Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18) E/flutter ( 6877): #27 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4536:16) E/flutter ( 6877): #28 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4684:11) E/flutter ( 6877): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:4227:5) E/flutter ( 6877): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4490:5) E/flutter ( 6877): #31 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4675:11) E/flutter ( 6877): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4485:5) E/flutter ( 6877): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14) E/flutter ( 6877): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18) E/flutter ( 6877): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5839:14) E/flutter ( 6877): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14) E/flutter ( 6877): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18) E/flutter ( 6877): #38 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5839:14) E/flutter ( 6877): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14) E/flutter ( 6877): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18) E/flutter ( 6877): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4536:16) E/flutter ( 6877): #42 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4684:11) E/flutter ( 6877): #43 Element.rebuild (package:flutter/src/widgets/framework.dart:4227:5) E/flutter ( 6877): #44 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4490:5) E/flutter ( 6877): #45 StatefulElement._fir
navigator.dart:2136の中を見ると、上記のメソッドはContextのメソッドなので
Navigator.push()をする処理が早く、Contextが良い状態になってないのかなと予想しています。
そのため、Contextが引数として貰える別の場所にて、Navigator.push処理を移動させてみようと考えています。
そこで、現在のcontextを引数として貰えるbuild()内やライフサイクルメソッド内で、
アクション(Navigator.pushの処理)を行う方法をご存知の方がいましたら、ご教授お願いしたく思います。
(ボタンプッシュによるアクション(例えばfloatingactionbuttonのonPressed())へ処理を移動させることはやりたくありません。)
camera.dart
1import 'dart:async'; 2//ファイル出力用ライブラリ 3import 'dart:io'; 4 5import 'package:camera/camera.dart'; 6import 'package:flutter/material.dart'; 7import 'package:image_picker/image_picker.dart'; 8import 'package:path_provider/path_provider.dart'; 9import 'package:path/path.dart' show context, join; 10 11import 'package:flutter/rendering.dart'; 12 13import 'package:http/http.dart' as http; 14 15import 'package:logging/logging.dart'; 16import 'package:fluttertoast/fluttertoast.dart'; 17 18import 'package:intl/intl.dart'; 19 20import 'package:flutter_gowjp/responseView.dart'; 21import 'package:permission_handler/permission_handler.dart'; 22 23CameraController mController = null; 24List<CameraDescription> mCameras = null; 25CameraDescription mFirstCamera = null; 26Future<void> mInitializeControllerFuture = null; 27String mResponseBody = ""; 28final mLog = new Logger('gowjp(camera)'); 29String mCashedImageName = ""; 30 31 32class CameraView extends StatefulWidget { 33 34 @override 35 State createState() => CameraState(); 36 37} 38 39class CameraState extends State<CameraView> { 40 41 final _mPicker = ImagePicker(); 42 BuildContext mContext = null; 43 44 // カメラを起動する 45 void _takePicture() async { 46 47 // ストレージアクセス権限のチェック 48 var status = await Permission.storage.status; 49 if (!status.isGranted) { 50 await Permission.storage.request(); 51 } 52 try { 53 final pickedFile = await _mPicker.getImage(source: ImageSource.camera); 54 final String nowTime = 'IMG_${DateFormat('yyyyMMddHHmmss').format(DateTime.now())}'; 55 final String cameraPath = '/storage/emulated/0/DCIM/Camera'; 56 57 // "OK"を押すと以下の処理が走る 58 // /data/のアプリ領域配下にある撮影画像のパス 59 String picFilePath = pickedFile.path; 60 61 setState(() { 62 // POST送信するClassへ遷移する 63 transitionTo(picFilePath, nowTime); 64 }); 65 66 } catch (e) { 67 print(e); 68 Fluttertoast.showToast(msg: "カメラ撮影で予期せぬエラーが発生しました。"); 69 return; 70 } 71 } 72 73 @override 74 void initState() { 75 super.initState(); 76 77 // カメラを起動する 78 _takePicture(); 79 } 80 81 @override 82 void didChangeDependencies() { 83 super.didChangeDependencies(); 84 } 85 86 @override 87 void dispose() { 88 super.dispose(); 89 } 90 91 // POST送信するClassへ遷移する 92 void transitionTo(String picFilePath, String picTime) async { 93 String dName = 'cameraPicture'; 94 95 Navigator.push( 96 mContext, 97 new MaterialPageRoute<Null>( 98 settings: const RouteSettings(name: "/responseView"), 99// builder: (BuildContext context) => ResponseView(picFilePath, dName), 100 builder: (BuildContext context) => ResponseView.part2(picFilePath, picTime, dName), 101 ), 102 ); 103 } 104 105 @override 106 Widget build(BuildContext context) { 107 // 黒画面 108 return Scaffold( 109 body: Container( 110 color: Colors.black, 111 ) 112 ); 113 114 } 115 116}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。