Flutter 中的 class MyApp 错误未定义方法 'setState'

The method 'setState' isn't defined for the class MyApp error in Flutter

我在 TextField

onSubmitted 中收到错误 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;
                  });
                },

            ),
          ),
        ),
      )
    );
   }

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

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

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

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

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

地方

setState

里面

StatefullWidget

:) 这将解决问题

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

每当您更改 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;
                  });
                },

            ),
          ),
        ),
      )
    );
   }
}

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

setState 仅在 StatefulWidget class/subclass 内可用。您需要将 StatelessWidget 转换为 StatefulWidget。只需:

点击StatelessWidget class并使用选项 + return(或cmd + . 如果你在 macOS 上使用 VS Code),

确保你输入正确

setState( () {} ) ;

如果您使用的是 lambda 并收到“setstate isn't referenced”,请务必在括号内正确输入:

setState(() => _counter++);

而不是:

setState() => _counter++;

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

class YourClassName extends StatelessWidget { 

有了这个:

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

class _YourClassNameState extends State<YourClassName> {

所有其他事情都是一样的,现在您可以在 class:

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

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

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

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

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