gpt4 book ai didi

flutter - 清除 TextField 时出错

转载 作者:IT王子 更新时间:2023-10-29 07:02:19 25 4
gpt4 key购买 nike

我正在尝试清除我的 TextField,这是我所做的

IconButton(
onPressed: (){
FocusScope.of(context).requestFocus(new FocusNode());
_searchFieldController.clear();
setState(() {
searchClicked = false;
});
},
icon: Icon(Icons.close),
);
}

当我运行它时出现这个错误

I/flutter ( 4547): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ I/flutter ( 4547): The following assertion was thrown while handling a gesture: I/flutter ( 4547): invalid text selection: TextSelection(baseOffset: 6, extentOffset: 6, affinity: I/flutter ( 4547): TextAffinity.upstream, isDirectional: false)

这是我的小部件

TextField(
onChanged: (text) {
if(text.length >= 4){
searchResult = productTemp.where((i) => i.productName.contains(text.toString())).toList();
_productController.sink.add(searchResult);
}else{
_productController.sink.add(productTemp);
}
},
focusNode: _focus,
controller: _searchFieldController,
style: TextStyle(fontSize: 15),
decoration: InputDecoration(
filled: true,
hintStyle:new TextStyle(color: Colors.grey[800],fontSize: 20),
fillColor: Colors.white70,
hintText: "Search",
suffixIcon: searchClicked ? buildCancelIcon() : buildSearchIcon()
),
)

最佳答案

https://github.com/flutter/flutter/issues/17647

WidgetsBinding.instance.addPostFrameCallback((_) => _searchFieldController.clear());

关于flutter - 清除 TextField 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57059516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com