请查看我的代码:
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):W小组件库引起的异常提示 ╞═════════════════════════════════════════════════ ══════════我/扑 (10662):在完成最终确定时抛出了以下断言 小部件树:I / flutter(10662):调用了setState()或markNeedsBuild() 小部件树被锁定时。 I / flutter(10662):此 无法将_ModalScope小部件标记为需要构建,因为该框架已被I / flutter(10662):锁定。 I /颤振(10662): 调用setState()或markNeedsBuild()的小部件为: I / flutter(10662):
_ModalScope- [LabeledGlobalKey <_ModalScopeState>#1f222](状态: I / flutter(10662):_ModalScopeState#6c40b)
如何正确做我想做的事?
答案 0 :(得分:0)
此错误表示您在构建阶段正在调用setState,而不能执行。