【问题标题】:The method 'setState' isn't defined for the class MyApp error in FlutterFlutter 中的 MyApp 类错误未定义方法“setState”
【发布时间】:2018-04-01 10:39:55
【问题描述】:

我在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;
                  });
                },

            ),
          ),
        ),
      )
    );
   }

【问题讨论】:

  • 你需要创建自己的StatefulWidget,在那里你可以调用setState
  • 我们可以在 Util 类中使用 setState 方法吗? Util 类不是有状态的小部件。

标签: flutter


【解决方案1】:

我假设您正在尝试在无状态小部件中设置状态,该小部件是不可变的(无法更改)。

使用有状态小部件来执行此操作,如下所示:

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

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

【讨论】:

  • 嗨 Oush,可能知道为什么我们需要这样做 'class MainPage extends StatefulWidget{ HomePage createState()=> HomePage(); }' 我目前在 HomePage() 上遇到了这个问题;
  • 嗨@FoolBaby,MainPage 在这种情况下只是一个例子
  • 是的,但你需要:"class MainPage extends StatefulWidget{ HomePage createState()=> HomePage();}" 这是因为我没有添加这个,flutter 仍然将 SetSate 作为未定义的方法,但我确实添加了,它有效
【解决方案2】:

地点

设置状态

里面

StatefullWidget

:) 这将解决问题

【讨论】:

    【解决方案3】:

    您必须在有状态小部件中调用该函数

    【讨论】:

      【解决方案4】:

      setState 仅在 StatefulWidget 类/子类中可用。您需要将StatelessWidget 转换为StatefulWidget。简单地说:

      点击StatelessWidget类并使用option + return(或者cmd + .如果你在 macOS 上使用 VS Code),

      【讨论】:

        【解决方案5】:

        确保你输入正确

        setState( () {} ) ;
        

        【讨论】:

          【解决方案6】:

          使用有状态的小部件来做到这一点

          每当您更改 State 对象的内部 state 时,请在您传递给 setState 的函数中进行更改:

          setState(() { _myState = newValue; });
          

          解决上面的问题,像这样:

          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;
                            });
                          },
          
                      ),
                    ),
                  ),
                )
              );
             }
          }
          

          【讨论】:

            【解决方案7】:

            将光标保持在StatelessWidget上方并按Alt + insert并点击转换为StatefulWidget,即可将您的StatelessWidget快速转换为StatefulWidget

            【讨论】:

              【解决方案8】:

              如果您使用 lambda 并得到“未引用 setstate”,请务必使用括号正确键入:

              setState(() => _counter++);
              

              代替:

              setState() => _counter++;
              

              【讨论】:

                【解决方案9】:

                如上所述,问题源于 StatelessWidget。对于使用文本编辑器进行编码的人来说,最简单的方法如下: 替换这个:

                class YourClassName extends StatelessWidget { 
                

                有了这个:

                class YourClassName extends StatefulWidget {
                  @override
                  _YourClassNameState createState() => _YourClassNameState();
                }
                
                class _YourClassNameState extends State<YourClassName> {
                

                所有其他事情都一样,现在你可以在你的类中调用 setState:

                setState(() {
                      whatever you want to update goes here
                    });
                

                但是,将 setState 放在自己的函数中并从任何地方轻松触发它更实用:

                void funtionName(passing parameters) {
                    setState(() {
                      ....
                    });
                }
                

                现在可以从任何地方轻松调用您的函数,例如:函数名称(参数)。

                【讨论】:

                  【解决方案10】:

                  setState{} 仅在 Stateful Widget 类/子类中可用。您需要将 Stateless Widget 转换为 StatefulWidget。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2020-05-27
                    • 1970-01-01
                    • 1970-01-01
                    • 2020-12-17
                    相关资源
                    最近更新 更多