我正在使用 Firebase 实时数据库创建 Flutter Web 应用程序。我只是在发布模式下得到了不同的结果,在发布模式下只有一个 Card 小部件返回。
我使用“列表项目”作为容器并将卡片放入其中。在调试模式下看起来不太好,但为什么在变成发布模式后gridView中只有一张卡。
调试模式命令:flutter run 释放模式命令:flutter run --release
Different between release mode and debug mode
将我的 Scaffold 类放置如下。
Scaffold(
appBar: AppBar(
title: Text('Sense'),
backgroundColor: Color.fromRGBO(133, 1, 132, 1),
),
body: StreamBuilder(
stream: databaseRef.onValue,
builder: (BuildContext context, AsyncSnapshot snap) {
if (!snap.hasError && snap.hasData) {
fb.DataSnapshot snapshot = snap.data.snapshot;
List<Widget> items = [];
snapshot.forEach((e) => items.add(Card(
color: Colors.grey[200],
elevation: 5,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: 50,
padding: EdgeInsets.all(10),
// decoration: BoxDecoration(
// border: Border.all(color: Colors.red)),
child: FittedBox(
child: Text(
e.key.toUpperCase(),
style: TextStyle(
letterSpacing: 3,
fontWeight: FontWeight.bold),
),
),
),
Expanded(
child: Container(
padding: EdgeInsets.all(10),
// decoration: BoxDecoration(
// border: Border.all(color: Colors.green)),
child: FittedBox(
child: Text(
e.val().toString(),
style: TextStyle(
color: Color.fromRGBO(133, 1, 132, 1)),
)))),
Container(
padding: EdgeInsets.all(10),
height: 50,
// decoration: BoxDecoration(
// border: Border.all(color: Colors.blue)),
child: FittedBox(
child: Text(
'Unit: ',
style: TextStyle(color: Colors.grey),
),
),
),
],
),
)));
return GridView.extent(
maxCrossAxisExtent: 480,
padding: EdgeInsets.all(5),
children: items);
} else {
return SpinKitDoubleBounce(
color: Color.fromRGBO(133, 1, 132, 1));
}
},
),
bottomNavigationBar: BottomAppBar(
color: Colors.transparent,
elevation: 0,
child: SvgPicture.asset('logo.svg'),
)),
);
答案 0 :(得分:0)
您的 StreamBuilder 需要指定对象。如果它是一个列表,只需检查 List.isNotEmpty
StreamBuilder<List<Object>>(
stream: databaseRef.onValue,// need to return : Stream<List<Object>>
builder: (context, snap) {
if (!snap.hasError && snap.hasData) {
final items = snap.data;
if(items.isNotEmpty){
...
}
}
});
答案 1 :(得分:0)
可以通过以下方法修复。
snapshot.forEach((e) => items.add(Card(...)))
到
snapshot.forEach((e) {items.add(Card(...))})
有人知道根本原因吗?