我使用listView Builder从Future中构建类别的ListView,调试完所有代码之后,除了未执行itemBuilder函数外,当我调试itemCount时:快照.data.length已执行但itemBuilder未执行,这是我的代码:
var categoryIcon = Container(
color: Colors.white,
padding: EdgeInsets.only(top: 20.0),
alignment: AlignmentDirectional.centerStart,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 20.0, top: 0.0),
child: Text(
"Categories ",
style: TextStyle(
fontSize: 13.5,
fontFamily: "Sans",
fontWeight: FontWeight.w700),
),
),
FutureBuilder(
future: getCategories(),
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.data == null){
return Container(
);
}
else{
return ListView.builder(
itemBuilder: (BuildContext context, int index){
return new Container(
child: new Text(snapshot.data[index].name)););
},
itemCount : snapshot.data.length
);
}
}
),
/// Get class CategoryIconValue
Padding(padding: EdgeInsets.only(bottom: 30.0))
],
),
);
这是我解析Json的函数:
Future<List<Category>> getCategories() async{
var data = await http.get("http://192.168.0.134:9091/Categorie");
var extractData = json.decode(data.body);
List<Category> listCat = [];
for(var c in extractData){
Category cat = new Category(c["id"],c["name"]);
listCat.add(cat);
}
print(listCat.length);
return listCat;
}
答案 0 :(得分:0)
如果您是Flutter中使用API的新手,那将是一本好书,您可以看一下Pooja撰写的this以及有关使用FutureBuilder进行操作的精彩文章。
希望获得帮助。