解決したい課題
テキストフィールドで入力した値を次の画面に表示させたい。
現状
Navigator operation requested with a context that does not include a Navigator.
実行ボタンを押すと下記エラーが表示される。
lib/main.dart:28:21: Error: The method 'setState' isn't defined for the class 'MyApp'. - 'MyApp' is from 'package:test20210123/main.dart' ('lib/main.dart'). Try correcting the name to the name of an existing method, or defining a method named 'setState'. setState((){ ^^^^^^^^
コード
//入力画面 import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { String name; @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: Scaffold( appBar: AppBar( title: Text('入力画面'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ TextField( onChanged: (value) { setState((){ name = value; }); }, decoration: InputDecoration( hintText: 'ここに入力した値を次の画面に渡したい' ), ), TextField( decoration: InputDecoration( hintText: 'ここに入力した値を次の画面に渡したい' ), ), RaisedButton( child: Text('確認画面へ'), onPressed: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => NextPage(name: name)), ); } ), ], ), ), ) ); } } //確認画面 class NextPage extends StatelessWidget{ @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: NextPage2(), ); } } class NextPage2 extends StatelessWidget{ NextPage2({this.name}); final String name; @override Widget build(BuildContext context){ return Scaffold( appBar: AppBar( title: Text('確認画面'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(name), Text(name), ], ), ), ); } }
以上、どなたか教えてくださると助かります。
お願いいたします。
追記
修正後のコード
//入力画面 import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { String name; @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: Scaffold( appBar: AppBar( title: Text('入力画面'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ TextField( onChanged: (value) { setState((){ name = value; }); }, decoration: InputDecoration( hintText: 'ここに入力した値を次の画面に渡したい' ), ), TextField( decoration: InputDecoration( hintText: 'ここに入力した値を次の画面に渡したい' ), ), RaisedButton( child: Text('確認画面へ'), onPressed: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Scaffold( appBar: AppBar( title: Text('確認画面'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(name), Text(name), ], ), ), ), ), ); } ), ], ), ), ) ); } } //確認画面 class NextPage extends StatelessWidget{ @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: NextPage2(), ); } } class NextPage2 extends StatelessWidget{ NextPage2({this.name}); final String name; @override Widget build(BuildContext context){ return Scaffold( appBar: AppBar( title: Text('確認画面'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(name), Text(name), ], ), ), ); } }
エラー
lib/main.dart:28:21: Error: The method 'setState' isn't defined for the class 'MyApp'. - 'MyApp' is from 'package:test20210124/main.dart' ('lib/main.dart'). Try correcting the name to the name of an existing method, or defining a method named 'setState'. setState((){ ^^^^^^^^
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/23 14:28
2021/01/23 14:32
2021/01/23 14:47
2021/01/23 15:03 編集
2021/01/23 15:23
2021/01/23 15:34
2021/01/24 03:10
2021/01/24 06:51