我正在尝试从实时Firebase检索数据。没有错误显示,但屏幕上仍然没有数据显示。我正尝试将数据显示在卡片中,如下图所示:
这是我的代码:
**List<Restaurant> restaurantsList = [];
@override
void initState() {
// TODO: implement initState
super.initState();
DatabaseReference restRef = FirebaseDatabase.instance.reference().child('restaurant');
restRef.once().then((DataSnapshot snap){
var keys = snap.value.keys;
var data = snap.value;
restaurantsList.clear();
for(var key in keys){
Restaurant rest = new Restaurant
(
data[key]['image'],
data[key]['name'],
data[key]['description'],
);
restaurantsList.add(rest);
}
setState(() {
print('$restaurantsList');
print('Length : $restaurantsList.length');
});
});
}
GridView.builder(
shrinkWrap: true,
primary: false,
physics: NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 1.25),
),
itemCount: restaurantsList == null ? 0 :restaurantsList.length,
itemBuilder: (BuildContext context, int index) {
return GridProduct(
img: restaurantsList[index].image,
isFav: false,
name: restaurantsList[index].name,
rating: 5.0,
raters: 23,
);
},
),