未为Flutter中的类MyApp错误定义方法'setState' [英] The method 'setState' isn't defined for the class MyApp error in Flutter

查看:471
本文介绍了未为Flutter中的类MyApp错误定义方法'setState'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TextFieldonSubmitted中收到错误The method 'setState' isn't defined for the class MyApp error in Flutter

代码:

  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.

I assume you are trying to setState in a stateless widget, which is immutable(unable to change).

使用有状态的小部件来这样做,就像这样:

Use a stateful widget to do so, like this:

class MainPage extends StatefulWidget{
  HomePage createState()=> HomePage();
}

class HomePage extends State<MainPage>{
 //Your code here
}

这篇关于未为Flutter中的类MyApp错误定义方法'setState'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆