Stream Builder窗口小部件显示的项目与Firebase数据库中的不一样。 我已经经历了以下解决方案:
但它没有帮助,如上面StreamBuilder中的链接中所示,未使用Firebase Realtime数据库。
在firebase中,另存为:
在模拟器上的结果:
列表未显示SubChild密钥,即日期与保存在Firebase数据库中的方式不同。
StreamBuilder窗口小部件代码为
new StreamBuilder(
stream: databaseReference.onValue,
builder: (context, snap) {
if (snap.connectionState == ConnectionState.active) {
if (snap.hasData &&
!snap.hasError &&
snap.data.snapshot.value != null) {
Map<dynamic, dynamic> data = snap.data.snapshot.value;
print("data is $data");
List item = [];
data.forEach((index, data)=>item.add({
"key":index, ...data
}));
//data.forEach((index, data) => item.add({"key": index, ...data}));
return item.length == 0
? Container(
alignment: FractionalOffset.center,
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.error,
size: 24.0,
),
new Text('No Data Found')
],
),
)
: Padding(
padding: const EdgeInsets.all(8.0),
child: GridView.builder(
physics: ClampingScrollPhysics(),
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: item.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio: 1.0,
mainAxisSpacing: 2.0,
crossAxisSpacing: 2.0,
),
itemBuilder: (BuildContext context, int index) =>new GestureDetector(
child: GestureDetector(
onTap: (){
Navigator.push(context,MaterialPageRoute(builder:(context)=>NavigationBarScreen(item[index]['key'].toString())));
},
child: new Card(
elevation: 4.0,
color: ConstantColors.PrimaryAssentColor,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.date_range,color: Colors.white,),
new Text(item[index]['key'],style: new TextStyle(color: Colors.white,fontFamily: 'BlacksOps',)),
],
),
),
),
)),
);
}