Flutter 如何理解用户何时删除文本输入?

Flutter 如何理解用户何时删除文本输入?,flutter,Flutter,我有一个小部件,从用户那里获取输入。当用户正常输入变量并按下保存按钮时,没有问题。当用户在提供一些输入后删除输入时,我的应用程序将中断。我的问题是:当用户删除输入并让我的应用程序正确处理时,我如何理解 Expanded( child: Container( decoration: BoxDecoration(

我有一个小部件,从用户那里获取输入。当用户正常输入变量并按下保存按钮时,没有问题。当用户在提供一些输入后删除输入时,我的应用程序将中断。我的问题是:当用户删除输入并让我的应用程序正确处理时,我如何理解

Expanded(
                                    child: Container(
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(5),
                                        color: Colors.grey.withOpacity(0.3),
                                      ),
                                      child: TextField(
                                        style: TextStyle(
                                          color: Colors.black,
                                          fontSize: 20,
                                        ),
                                        textAlign: TextAlign.center,
                                        textAlignVertical: TextAlignVertical.center,
                                        onChanged: (value){
                                            setState(() {
                                              _ingredients[index].ingredientAmount=value;
                                              categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].ingredients[index].ingredientAmount=value;
                                              var flag=0;
                                              for(int i=0;i<=_ingredients.length-1;i++){
                                                if(_ingredients[i].ingredientAmount==null){
                                                 flag=-1;
                                                }
                                              }
                                              if(flag==-1){
                                                falseIngredientAmountSelected(context);
                                              }
                                              else{
                                                trueIngredientAmountSelected(context);
                                              }
                                            });
                                        },
                                        decoration: InputDecoration(
                                          border:OutlineInputBorder(
                                            borderRadius: BorderRadius.circular(5),
                                            borderSide: BorderSide(
                                            ),
                                          ),
                                          hintText: "Miktar Girin",
                                          hintStyle: TextStyle(
                                            color: Colors.black,
                                            fontSize: 14,
                                          ),
                                        ),
                                      ),
                                      height: 50,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                )
扩展(
子:容器(
装饰:盒子装饰(
边界半径:边界半径。圆形(5),
颜色:颜色。灰色。不透明度(0.3),
),
孩子:TextField(
样式:TextStyle(
颜色:颜色,黑色,
尺寸:20,
),
textAlign:textAlign.center,
textAlignVertical:textAlignVertical.center,
一旦更改:(值){
设置状态(){
_成分[索引].InGedentaMount=值;
categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].Components[index].IngredentAmount=value;
var标志=0;

对于(int i=0;i在
onChanged
中,首先检查
if(value.isNotEmpty)
。如果
value.isNotEmpty
返回false,您可以通过输入
return;
中止将要对空值执行的任何操作。您还可以显示一条消息,告知用户输入不应为空

我还建议使用
onSubmitted
onEditingComplete
而不是
onChanged
,这样它只在用户完成文本输入时检查或执行某些操作,而不是在每个字符之后