我不知道为什么在Flutter中出现setstate错误?

huangapple go评论71阅读模式
英文:

I dont know why error on setstete in flutter?

问题

它在Flutter中的setstate上显示错误,但写得正确。请帮忙纠正它。

错误在setstate上
TextButton(onPressed: (){
  setState((){
    naveen++;
    print(naveen);
  });
}, child: Text('button')),
Text("$naveen")
英文:

It shows an error on the setstate in flutter but wrote correctly. Kindly help to rectify it.

error on setstate

我不知道为什么在Flutter中出现setstate错误?

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.white,
      ),
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
   MyHomePage({Key? key}) : super(key: key);
  var naveen = 1;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("hello"),
      ),
      body: Center(
        child: Column(
         children: [
           TextButton(onPressed: (){
             setState((){
             naveen++;
             print(naveen);
           });
           }, child: Text('button')),
           Text("$naveen")
         ],

        ),
      ),
    );
  }
}

It shows an error on the setstate in flutter but wrote correctly. Kindly help to rectify it.

答案1

得分: 1

你应该使用 StatefulWidget 来使用 setState

你的 MyHomePage 应该像这样...

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var naveen = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("hello"),
      ),
      body: Center(
        child: Column(
          children: [
            TextButton(onPressed: () {
              setState(() {
                naveen++;
                print(naveen);
              });
            }, child: Text('button')),
            Text("$naveen")
          ],
        ),
      ),
    );
  }
}
英文:

You should use StatefulWidget to use setState.

Your MyHomePage should be like..


class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  var naveen = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(&quot;hello&quot;),
      ),
      body: Center(
        child: Column(
          children: [
            TextButton(onPressed: (){
              setState((){
                naveen++;
                print(naveen);
              });
            }, child: Text(&#39;button&#39;)),
            Text(&quot;$naveen&quot;)
          ],

        ),
      ),
    );
  }
}

答案2

得分: 1

setState 是在 Flutter 中仅在有状态小部件中可用的方法。无状态小部件是不可变的,没有任何内部状态,因此您不能在其中调用 setState。

如果您想要更新小部件的状态,应该使用有状态小部件。在有状态小部件中,您可以定义一个保存小部件的可变状态的状态对象。然后,您可以使用 setState 方法来更新状态,从而触发小部件的重建。

如果您已经创建了一个无状态小部件,并且希望将其转换为有状态小部件,可以按照以下步骤操作:

  1. 创建一个新类,该类扩展 StatefulWidget。

  2. 将无状态小部件的代码移动到新类的 build 方法中。

  3. 创建一个新类,该类扩展 State,并使用它来保存小部件的可变状态。

  4. 在您的新有状态小部件类中覆盖 createState 方法,并返回刚刚创建的状态类的实例。

  5. 更新您的小部件树,以使用新的有状态小部件代替旧的无状态小部件。

英文:

setState is a method that is available only in stateful widgets in Flutter. Stateless widgets are immutable and don't have any internal state, so you cannot call setState in them.

If you want to update the state of a widget, you should use a stateful widget instead. In a stateful widget, you can define a state object that holds the mutable state of the widget. You can then use the setState method to update the state, which triggers a rebuild of the widget.

If you have already created a stateless widget and you want to convert it into a stateful widget, you can do the following:

  1. Create a new class that extends StatefulWidget.

  2. Move the code of your stateless widget into the build method of the
    new class.

  3. Create a new class that extends State and use it to hold the mutable
    state of the widget.

  4. Override the createState method in your new stateful widget class
    and return an instance of the state class you just created.

  5. Update your widget tree to use the new stateful widget instead of
    the old stateless widget.

huangapple
  • 本文由 发表于 2023年2月24日 16:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定