【发布时间】:2018-09-04 22:55:29
【问题描述】:
我的应用很简单。
我有一个主页,您可以在其中按一个按钮打开一个新的 MaterialPageRoute。
在这个新的页面路由中,我有一个基本的下拉菜单。
问题是,当我点击下拉菜单时,我得到了这个错误:
setState() or markNeedsBuild() called when widget tree was locked.
This Navigator widget cannot be marked as needing to build because the framework is locked.
下拉列表是“FilterPage”类的成员。仅当我将 FilterPage 放入新页面路由时才会发生错误。例如,如果我使用新的 FilterPage() 作为 home,那么一切正常。
复制它的所有代码如下。谢谢你看这个!
import 'package:flutter/material.dart';
final Map<String,String> categoryLookup = {
"business":"Accounting/Business",
"science":"Science",
"technology":"Technology"
};
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Link',
home: new Home(),
routes: <String, WidgetBuilder> {
'/filter': (BuildContext context) => new FilterPage(),
},
);
}
}
class Home extends StatefulWidget {
@override
createState() => new HomeState();
}
class HomeState extends State<Home> {
@override
Widget build(BuildContext context){
return new Scaffold(
appBar: new AppBar(
title: new Text('Browse Jobs'),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.search),
onPressed: () => Navigator.of(context).pushNamed('/filter'),
),
],
),
body: new Center(
child: new Text("Home"),
),
);
}
}
class FilterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final GlobalKey<MyDropDownState> categoryKey = new GlobalKey<MyDropDownState>();
final MyDropDown categoryDropdown = new MyDropDown(key: categoryKey, itemMap: categoryLookup);
print("key: "+ categoryKey.toString());
return new Scaffold(
appBar : new AppBar(
title: new Text('Filter Jobs'),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.refresh),
onPressed: () {
print('Will reset dropdown using key');
},
),
],
),
body: new Container(
padding: new EdgeInsets.all(20.0),
child: new Column(
children: <Widget>[
categoryDropdown,
],
),
),
);
}
}
class MyDropDown extends StatefulWidget {
MyDropDown({
Key key,
this.itemMap
}) : super(key:key);
final Map<String, String> itemMap;
@override
MyDropDownState createState() => new MyDropDownState();
}
class MyDropDownState extends State<MyDropDown> {
String _selection;
@override
Widget build(BuildContext context) {
return new DropdownButton(
value: _selection,
items: getDropItems(widget.itemMap),
onChanged: (s) {
setState(() {
_selection = s;
});
},
hint: new Text('None'),
);
}
List<DropdownMenuItem> getDropItems(Map<String, String> itemMap){
final List<DropdownMenuItem> itemList = [];
itemMap.forEach(
(key,value){
itemList.add(new DropdownMenuItem<String>(value: key, child: new Text(value)));
}
);
return itemList;
}
}
【问题讨论】: