我正在开发一个颤振应用程序,我发现如何从 Firebase 读取 product_name 和 price 并将它们显示在列表中
我尝试了这段代码,但它将整个 JSON 树作为文本返回
class _BeveragesProductsState extends State<BeveragesProducts> {
var _firebaseRef = FirebaseDatabase().reference().child('Products');
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text(' Beverages Products'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: StreamBuilder(
stream: _firebaseRef.onValue,
builder: (context, snap) {
if (snap.hasData &&
!snap.hasError &&
snap.data.snapshot.value != null) {
Map data = snap.data.snapshot.value;
List item = [];
print("data $data");
data.forEach(
(index, data) => item.add({"key": index, ...data}));
print(item);
return ListView.builder(
reverse: true,
itemCount: item.length,
itemBuilder: (context, index) {
return Column(
children: <Widget>[
Text("$data"),
],
);
});
}
},
),
),
],
),
);
}
}