#実現したいこと
親ウィジェットから関数を渡していって子WidgetのRaisedButton
のonPressed
部分で呼び出したいです。
#環境
Flutter 1.17.4
Dart 2.8.6
VSCode 1.46.1
macOS Catalina 10.15.5
#エラー内容
ビルド、実行はできますが、RaisedButton
を押した時にDEBUG CONSOLE内で以下のエラーが表示され、本来呼び出したい関数が呼び出されない。
═══════ Exception caught by gesture ═══════════════════════════════════════════ The following NoSuchMethodError was thrown while handling a gesture: The method 'call' was called on null. Receiver: null Tried calling: call() When the exception was thrown, this was the stack #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5) #1 _MemberPageState.build.<anonymous closure> package:day_log/main.dart:74 #2 HomeView.build.<anonymous closure> package:day_log/home.dart:42 #3 _InkResponseState._handleTap package:flutter/…/material/ink_well.dart:779 #4 _InkResponseState.build.<anonymous closure> package:flutter/…/material/ink_well.dart:862 ... Handler: "onTap" Recognizer: TapGestureRecognizer#c42bd debugOwner: GestureDetector state: possible won arena finalPosition: Offset(207.0, 456.0) finalLocalPosition: Offset(45.5, 17.5) button: 1 sent tap down ════════════════════════════════════════════════════════════════════════════════
#コード
main関数はこちらです。
main
1import 'package:flutter/material.dart'; 2import 'home.dart'; 3import 'Login.dart'; 4 5void main() { 6 runApp(MyApp()); 7} 8 9class MyApp extends StatelessWidget { 10 @override 11 Widget build(BuildContext context) { 12 return MaterialApp( 13 title: 'Example', 14 theme: ThemeData.dark(), 15 home: MemberPage(), 16 ); 17 } 18} 19 20enum AuthStatus { 21 nonSignedIn, 22 signedIn, 23} 24 25class RootPage extends StatefulWidget { 26 @override 27 _RootPageState createState() => _RootPageState(); 28} 29 30class _RootPageState extends State<RootPage> { 31 AuthStatus authStatus = AuthStatus.nonSignedIn; 32 33 void _signedIn() { 34 setState(() { 35 authStatus = AuthStatus.signedIn; 36 }); 37 } 38 39 void _signedOut() { //呼び出したい関数はこれです 40 print(authStatus); 41 setState(() { 42 authStatus = AuthStatus.nonSignedIn; 43 }); 44 print('authStatus is :'); 45 print(authStatus); 46 } 47 48 @override 49 Widget build(BuildContext context) { 50 switch (authStatus) { 51 case AuthStatus.nonSignedIn: 52 return LoginPage(onSignIn: _signedIn); 53 case AuthStatus.signedIn: 54 return MemberPage(signedOut: _signedOut); //ここでMemberPage widgetに渡しています 55 default: 56 return null; 57 } 58 } 59} 60 61class MemberPage extends StatefulWidget { 62 MemberPage({this.signedOut}); 63 final VoidCallback signedOut; 64 @override 65 _MemberPageState createState() => _MemberPageState(); 66} 67 68class _MemberPageState extends State<MemberPage> { 69 int _selectedPage = 0; 70 71 @override 72 Widget build(BuildContext context) { 73 final _pageOptions = [ 74 HomeView(signedOut: widget.signedOut), //ここでさらにHomeView widgetに渡しています 75 Center(child: Text('Search')), 76 Center(child: Text('Featured')), 77 Center(child: Text('Setting')), 78 ]; 79 return Scaffold( 80 body: _pageOptions[_selectedPage], 81 bottomNavigationBar: BottomNavigationBar( 82 currentIndex: _selectedPage, 83 onTap: (int index) { 84 setState(() { 85 _selectedPage = index; 86 }); 87 }, 88 items: [ 89 BottomNavigationBarItem( 90 icon: Icon(Icons.home), 91 title: Text('Home'), 92 ), 93 BottomNavigationBarItem( 94 icon: Icon(Icons.search), 95 title: Text('Search'), 96 ), 97 BottomNavigationBarItem( 98 icon: Icon(Icons.featured_play_list), 99 title: Text('Featured'), 100 ), 101 BottomNavigationBarItem( 102 icon: Icon(Icons.settings), 103 title: Text('Setting'), 104 ), 105 ] 106 ), 107 ); 108 } 109}
HomeView
widgetのRaisedButton
から_signedOut
関数を呼び出したいです。
HomeView
widgetはこちらです。
Home
1import 'package:flutter/material.dart'; 2 3class HomeView extends StatelessWidget { 4 HomeView({this.signedOut}); 5 final VoidCallback signedOut; 6 7 @override 8 Widget build(BuildContext context) { 9 final width = MediaQuery.of(context).size.width; 10 return Center( //↓でVoidCallbackを実行 11 child: RaisedButton(child: Text('Sign Out'), onPressed: () => signedOut()), 12 ); 13 } 14} 15
もしよろしければお力添えをお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/26 04:52