Flutter中未为类MyApp错误定义方法'setState'
我正在错误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
}
以上是 Flutter中未为类MyApp错误定义方法'setState' 的全部内容, 来源链接: utcz.com/qa/434943.html