gpt4 book ai didi

exception - setState() 或 markNeedsBuild() 被调用 > 当 widget 树被锁定时。改变状态时

转载 作者:IT王子 更新时间:2023-10-29 07:04:09 26 4
gpt4 key购买 nike

请看我的代码:

class HomePageState extends State<HomePage> {
bool _isLoading = false;

.....

@override
Widget build(BuildContext context) {

var drawerOptions = <Widget>[];
......

drawerOptions.add(new ListTile(
leading: new Icon(Icons.account_balance),
title: new Text(Strings.menu_change_city),
onTap: () => createDialog()
));


if(_isLoading) return buildBusyForm();

return Scaffold( .... //window content
}

}

所以我有抽屉导航。一项(“选择城市”)不关闭抽屉导航,它显示选择城市对话框:

createDialog() {

setState(() {_isLoading = true;});

fetchCities().then((response) {

setState(() {_isLoading = false;});

showDialog(
context: context,
builder: (context) => CityChoiceDialog<City>(
title: Text(Strings.menu_change_city),
items: response,
initialValue: response.firstWhere((c) => c.id == globals.cityId, orElse: () => new City()),
itemBuilder: (City city) => Text(city.name),
onSelected: _onSelected,
onSubmitted: _onSubmitted));
});
}

所以,基本上它的目的是显示繁忙的表单,加载城市,然后隐藏繁忙的表单并显示城市列表对话框。它似乎有效,但我遇到了异常:

I/flutter (10662): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (10662): The following assertion was thrown while finalizing the widget tree: I/flutter (10662): setState() or markNeedsBuild() called when widget tree was locked. I/flutter (10662): This _ModalScope widget cannot be marked as needing to build because the framework is I/flutter (10662): locked. I/flutter (10662): The widget on which setState() or markNeedsBuild() was called was: I/flutter (10662):
_ModalScope-[LabeledGlobalKey<_ModalScopeState>#1f222](state: I/flutter (10662): _ModalScopeState#6c40b)

如何正确地做我想做的事?

最佳答案

此错误意味着您在构建阶段调用了 setState,您不能这样做。

关于exception - setState() 或 markNeedsBuild() 被调用 > 当 widget 树被锁定时。改变状态时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55038765/

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