我是新手,但遇到setState()之后Build不刷新的问题。在我的StatefulWidget中,一旦我拥有一个类别列表,我就必须在数据库中查询类别列表,我必须显示数据的ListView。我通过了代码,看是否有人可以帮我打印可变负载的图片
class _ListCategoriesState extends State<ListCategories> {
List<Category> _listCategories = new List();
List<Article> _listArticles = new List();
bool _loading = true;
@override
void initState() {
super.initState();
doRequest();
}
void doRequest() async {
getCategories().then((result) {
setState(() {
_listCategories = result;
_loading = false;
});
}).catchError((error) {
print(error);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(appBar: headerNav("Categorías"), body: _crearLista());
}
Widget _crearLista() {
print(_loading);
if (_loading) {
return new Center(child: new CircularProgressIndicator());
} else {
return ListView.builder(
itemCount: _listCategories.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(_listCategories[index].description),
trailing: Icon(Icons.keyboard_arrow_right),
);
},
);
}
}
印刷品显示我为真,然后为假
如果有人可以帮助我也理解这个问题所必须使用的概念,那么我的应用程序的所有小部件中都会包含该概念。
谢谢!
答案 0 :(得分:1)
在FutureBuilder
正文中使用Scaffold
body: FutureBuilder<List<Category>>(
future: getCategories(),
builder: (context, snap) {
if (snap.data == null) {
return Center(child: new CircularProgressIndicator());
}
return ListView.builder(/* ... */);
},
);