Flutter 方法';设置状态';isn';为颤振中的MyApp错误类定义了t
我得到的错误Flutter 方法';设置状态';isn';为颤振中的MyApp错误类定义了t,flutter,Flutter,我得到的错误在文本字段的提交的中,没有为颤振中的MyApp error类定义方法“setState” 代码: Widget build(BuildContext context) { String phoneNo; return new MaterialApp( title: 'SchoolTrack', theme: new ThemeData( primaryColor: Colors.grey[50], ),
在文本字段的提交的中,没有为颤振中的MyApp error类定义方法“setState”
代码:
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;
});
},
),
),
),
)
);
}
您必须在有状态小部件中调用该函数我假设您试图在无状态小部件中设置state,它是不可变的(无法更改)
使用有状态小部件执行此操作,如下所示:
class MainPage extends StatefulWidget{
HomePage createState()=> HomePage();
}
class HomePage extends State<MainPage>{
//Your code here
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
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;
});
},
),
),
),
)
);
}
}
class主页扩展StatefulWidget{
主页createState()=>主页();
}
类主页扩展状态{
//你的代码在这里
}
地点
设定状态
里面
状态控件
:)这将解决问题使用有状态小部件来解决问题
无论何时更改状态对象的内部状态
,都要在传递给设置状态
的函数中进行更改:
setState(() { _myState = newValue; });
解决上述问题,如下所示:
class MainPage extends StatefulWidget{
HomePage createState()=> HomePage();
}
class HomePage extends State<MainPage>{
//Your code here
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
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;
});
},
),
),
),
)
);
}
}
类MyApp扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
小部件构建(构建上下文){
字符串phoneNo;
返回新材料PP(
标题:"学校跑道",,
主题:新主题数据(
原色:颜色。灰色[50],
),
家:新脚手架(
appBar:null,
背景颜色:颜色。青色[100],
主体:新容器(
填充:常数边集全部(32.0),
孩子:新中心(
孩子:新文本字段(
自动对焦:对,
自动更正:错误,
装饰:新的输入装饰(
hintText:'键入电话号码',
后缀图标:新图标(Icons.send),
后缀样式:新的文本样式(
颜色:颜色。青色[300],
)
),
OnSubmited:(字符串输入){
设置状态(){
phoneNo=输入;
});
},
),
),
),
)
);
}
}
将光标保持在无状态小部件上方,按Alt+insert
并单击convert toStatefulWidget
,以快速将您的无状态小部件
转换为StatefulWidget
设置状态
仅在StatefulWidget
类/子类中可用。您需要将无状态小部件
转换为状态小部件
。简单地说:
单击无状态小部件
类并使用option+return(或cmd+。如果在macOS上使用VS代码)
确保键入的内容正确
setState( () {} ) ;
如果您正在使用lambda并获得“setstate未被引用”,请确保使用括号正确键入:
setState(() => _counter++);
而不是:
setState() => _counter++;
如上所述,问题源于无状态控件。对于使用文本编辑器进行编码的人,最简单的方法如下:
替换此项:
class YourClassName extends StatelessWidget {
为此:
class YourClassName extends StatefulWidget {
@override
_YourClassNameState createState() => _YourClassNameState();
}
class _YourClassNameState extends State<YourClassName> {
但是,更实际的做法是将setState放在自己的函数中,并从任何地方轻松触发它:
void funtionName(passing parameters) {
setState(() {
....
});
}
现在可以从任何地方轻松调用函数,如:FunctionName(parameter)。您需要创建自己的StatefulWidget,在那里您可以调用setState
我们可以在Util类中使用setState方法吗?Util类不是有状态小部件。您好,Osh,可能知道我们为什么需要这样做“类主页扩展有状态小部件{HomePage createState()=>HomePage();}”我目前在HomePage()上遇到了这个问题;Hi@傻瓜宝贝,MainPage
只是本例中的一个示例是的,但您需要:“类MainPage扩展StatefulWidget{HomePage createState()=>HomePage();}”这是因为我没有添加它,flutt仍然将setstate作为未定义的方法,但我添加了,它可以工作