我正在错误The method 'setState' isn't defined for the class MyApp error in Flutter的onSubmitted的TextField
The method 'setState' isn't defined for the class MyApp error in Flutter
onSubmitted
TextField
码:
Widget build(BuildContext context) { String phoneNo; return new MaterialApp( title: 'SchoolTrack', theme: new ThemeData( primaryColor: Colors.grey[50], ), home: new Scaffold( appBar: null, backgroundColor: Colors.cyan[100], body: new Container( padding: const EdgeInsets.all(32.0), child: new Center( child: new TextField( autofocus: true, autocorrect: false, decoration: new InputDecoration( hintText: 'Type the phone no', suffixIcon: new Icon(Icons.send), suffixStyle: new TextStyle( color: Colors.cyan[300], ) ), onSubmitted: (String input) { setState(() { phoneNo = input; }); }, ), ), ), ) ); }
我假设您正在尝试在无状态小部件中设置setState,这是不可变的(无法更改)。
使用有状态的小部件来这样做,如下所示:
class MainPage extends StatefulWidget{ HomePage createState()=> HomePage(); } class HomePage extends State<MainPage>{ //Your code here }