首页 > 解决方案 > setState() 或 markNeedsBuild() 在动态字段中的 onChange 函数期间在构建期间调用

问题描述

我试图在我使用setstate()的地方更新动态字段onChanged ,但我无法更改 controller.text。我正在使用 api 从后端获取数据并在变量列表中的字段中显示数据,但我无法编辑该字段,可能是由于此错误,我在Listview builder中获得了动态字段,然后定义了控制器并保存更改中的值一个Map<String,dynamic> 我的代码是-

 Container(
              width: double.infinity,
              color: Colors.white,
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text("Varient", style: TextStyle(fontSize: 16, color: AppColor.textDarkColor),),
                    SizedBox(height: 8,),
                    Container(
                      width: double.infinity,
                      color: Colors.white,
                      height: 250,
                      child: Form(
                        key: _formKey,
                        child: GridView.builder(
                            gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                                maxCrossAxisExtent: 200,
                                childAspectRatio: 6 / 2, crossAxisSpacing: 20,
                                mainAxisSpacing: 0),
                            itemCount: allAttributes.isEmpty ? 0 : allAttributes.length,
                            itemBuilder: (BuildContext context, int index) {
                              _controllers.add(new TextEditingController());
                              if(allAttributes[index].label == 'MRP'){
                                widget.productId!=null ?
                                _controllers[index].text = variantfirst[0].mrpPrice:
                                _controllers[index].text = '';
                              }else  if(allAttributes[index].label == 'Discount'){
                                widget.productId!=null ?
                                _controllers[index].text = variantfirst[0].discount:
                                _controllers[index].text = '';
                              }else  if(allAttributes[index].label == 'Sort'){
                                widget.productId!=null ?
                                _controllers[index].text = variantfirst[0].sort:
                                _controllers[index].text = '';
                              }else  if(allAttributes[index].label == 'SKU'){
                                widget.productId!=null ?
                                _controllers[index].text = variantfirst[0].sku:
                                _controllers[index].text = '';
                              }else  if(allAttributes[index].label == 'Weight'){
                                widget.productId!=null ?
                                _controllers[index].text = variantfirst[0].weight:
                                _controllers[index].text = '';
                              }else  if(allAttributes[index].label == 'Unit'){
                                widget.productId!=null ?
                                _controllers[index].text = variantfirst[0].unitType:
                                _controllers[index].text = '';
                              }
                              else if(allAttributes[index].label == 'Price'){
                                widget.productId!=null ?
                                _controllers[index].text = variantfirst[0].price:
                                _controllers[index].text = toPrice;
                                myObject.update(allAttributes[index].variantFieldName, (v) => toPrice, ifAbsent: () => toPrice);
                              }
                              return TextFormField(
                                textInputAction: TextInputAction.done,
                                cursorColor: AppColor.primaryColor,
                                style: TextStyle(
                                  color: AppColor.textDarkColor, fontSize: 16.0,
                                ),
                                validator: (val){
                                  if(allAttributes[index].validation == "true" ){
                                    if(val.isEmpty){
//                                  EasyLoading.showToast(allAttributes[index].label+' is Empty',
//                                      toastPosition: EasyLoadingToastPosition.bottom);
//                              return ;
                                      return allAttributes[index].label +" is empty";
                                    }
                                    return null ;
                                  }
                                },
                                onChanged: (val){
                                    setState(() {
                                      if(allAttributes[index].label=='MRP'){
                                        toPrice = mrp = val+".00";
                                        print(toPrice);
                                        myObject.update(allAttributes[index].variantFieldName, (v) => mrp, ifAbsent: () => mrp);
                                      }else if(allAttributes[index].label=='Discount'){
                                        discount = val+".00";
                                        double result = double.parse(mrp) / double.parse(discount);
                                        double resultno =  double.parse(mrp) - result;
                                        toPrice = resultno.toString();
                                        print(toPrice);
                                        myObject.update(allAttributes[index].variantFieldName, (v) => val, ifAbsent: () => val);
                                      }else if(allAttributes[index].label=='Price'){
                                        _controllers[index].text = toPrice;
                                        myObject.update(allAttributes[index].variantFieldName, (v) => toPrice, ifAbsent: () => toPrice);
                                      }
                                      else{
                                        myObject.update(allAttributes[index].variantFieldName, (v) => val, ifAbsent: () => val);
                                      }
                                    });
                                 

                                },
                                keyboardType: allAttributes[index].type =='number'
                                    ? TextInputType.number : TextInputType.text ,
                                controller: _controllers[index],
                                maxLines: 1,
                                decoration: InputDecoration(
                                    contentPadding: const EdgeInsets.all(0.0),
                                    labelText: allAttributes[index].label,
                                    hintStyle: TextStyle(color: AppColor.textLightColor)),
                              );
                            }
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),

标签: flutterdartflutter-layoutflutter-dependenciesdart-pub

解决方案


推荐阅读