简体   繁体   English

如何解决错误:未定义“setState”

[英]How to solve error: 'setState' isn't defined

Can you please help me to solve this problem about setState()你能帮我解决这个关于setState()的问题吗

ERROR:错误:
The method 'setState' isn't defined for the type 'MyApp'.没有为“MyApp”类型定义方法“setState”。
Try correcting the name to the name of an existing method, or defining a method named 'setState'.尝试将名称更正为现有方法的名称,或定义名为“setState”的方法。

CODE:代码:

return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text('Gestion Produit', ),
          backgroundColor: color1,
        ),
        backgroundColor: color2,
        body: SingleChildScrollView(
          child: Column(
            children: < Widget > [

              Container(
                padding: EdgeInsets.all(20),
                margin: EdgeInsets.fromLTRB(20, 20, 20, 20),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  color: color3,
                ),
                child: Column(
                  children: [
                    Container(
                      width: double.infinity,
                      height: 30,
                      child: TextFormField(
                        controller: refCon,
                        decoration: new InputDecoration(
                          disabledBorder: InputBorder.none,
                          contentPadding:
                          EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
                          hintText: "Votre Reference",
                          hintStyle: TextStyle(color: color1),
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    Container(
                      width: double.infinity,
                      height: 30,
                      child: TextFormField(
                        controller: qteCon,
                        keyboardType: TextInputType.number,
                        decoration: new InputDecoration(
                          disabledBorder: InputBorder.none,
                          contentPadding:
                          EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
                          hintText: "Votre Quantite",
                          hintStyle: TextStyle(color: color1),
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    Container(
                      width: double.infinity,
                      height: 30,
                      child: TextFormField(
                        controller: puCon,
                        keyboardType: TextInputType.number,
                        decoration: new InputDecoration(
                          disabledBorder: InputBorder.none,
                          contentPadding:
                          EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
                          hintText: "Prix Unitaire",
                          hintStyle: TextStyle(color: color1),
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    RaisedButton(
                      onPressed: () {
                          setState((){
                            ref = refCon;
                            qte = qteCon;
                            pu = puCon;
                          });
                      },
                      color: color2,
                      textColor: color3,
                      disabledColor: Colors.grey,
                      disabledTextColor: Colors.black,
                      padding: EdgeInsets.all(8.0),
                      splashColor: color1,
                      child: Text(
                        "Ajouter Un Produit",
                        style: TextStyle(fontSize: 20.0),
                      ),
                    )
                  ],
                ),
              ),

You widget in not stateful.你的小部件不是有状态的。 SetState method can be only used in stateful widgets. SetState 方法只能在有状态的小部件中使用。

Flutter widgets are of 2 types - Flutter 小部件有 2 种类型 -

  1. StatelessWidget - They do not have a State object associated with them. StatelessWidget - 他们没有关联的State object。 Once created/rendered, they can't be mutated.一旦创建/渲染,它们就不能被改变。 They will have to be rebuilt again他们将不得不再次重建

  2. StatefulWidget - A State object is associated with them. StatefulWidget - 一个State object 与它们相关联。 You would have to create another class extending the State class.您必须创建另一个 class 来扩展State class。

class YellowBird extends StatefulWidget {
  @override
  _YellowBirdState createState() => _YellowBirdState();
}

class _YellowBirdState extends State<YellowBird> {
  @override
  Widget build(BuildContext context) { }
}

Checkout this Medium article查看这篇中等文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决错误 The operator [] is not defined for the type &#39;object&#39; - How to solve error The operator [] isn't Defined for the type 'object' function 'setState' 未定义 - The function 'setState' isn't defined flutter setState 未定义 - flutter setState isn't defined Flutter 中的 MyApp 类错误未定义方法“setState” - The method 'setState' isn't defined for the class MyApp error in Flutter Flutter 错误 - 未为该类型定义方法“setState” - Flutter Error - Method 'setState' isn't defined for the type 固定错误:如何解决返回类型'StreamController<connectivitystatus> ' 不是匿名闭包错误所定义的 'Stream'</connectivitystatus> - Fixed-error: How to Solve The return type 'StreamController<ConnectivityStatus>' isn't a 'Stream', as defined by anonymous closure error 没有为“NasabahDataTableSource”类型定义方法“setState” - The method 'setState' isn't defined for the type 'NasabahDataTableSource' 没有为类型“pickImage”定义方法“setState” - The method 'setState' isn't defined for the type 'pickImage' 如何解决“返回类型&#39;DocumentReference()&#39;不是&#39;DocumentReference()&#39;,由方法”错误定义? - How to solve “The return type 'DocumentReference ()' isn't a 'DocumentReference ()', as defined by the method” error? 如何解决未定义命名参数“填充”。 未定义命名参数“形状” - How to solve The named parameter 'padding' isn't defined. The named parameter 'shape' isn't defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM