【发布时间】:2020-05-08 06:15:33
【问题描述】:
为什么在我的屏幕上出现错误Another exception was thrown: FormatException: Invalid number (at character 1) 几微秒后才一切恢复正常。有时甚至不会发生。下面是我的 StreamBuilder 函数:
_delivered() {
print('In the delivered function:${resId},${customerId}, ');
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('restaurants')
.document(resId)
.collection('customers')
.document(customer)
.collection('orders')
.where('deliveryTime', isGreaterThan: '')
.snapshots(),
builder: (context, snapshot) {
print('Does snapshop have data? ${snapshot.hasData}');
if (!snapshot.hasData) return Container();
List deliveredListFromServer = snapshot.data.documents;
return Expanded(
child: ListView(
shrinkWrap: true,
children: deliveredListFromServer.map((item) {
print('document id: ${item.documentID}');
return InkWell(
child: SizedBox(
height: 50,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
width: 80,
child: Text(
item['orderBy']['username'],
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold),
),
),
SizedBox(
width: 5,
),
Expanded(
child: ListView(
scrollDirection: Axis.horizontal,
children: item['order'].map<Widget>((item) {
return SizedBox(
width: 80,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
'${item['qty']} ${item['drinkName']}',
overflow: TextOverflow.ellipsis,
),
),
);
}).toList(),
), //
),
SizedBox(
width: 5,
),
SizedBox(
width: 60,
child: Text(DateFormat('h:mm a').format(
DateTime.fromMillisecondsSinceEpoch(
int.parse(item['deliveryTime'])))),
)
],
),
),
onTap: () {
_deliveredDetail(item);
},
);
}).toList(),
),
);
});
}
这是我的控制台:
I/flutter (11506): In the delivered function:XufIsxA8a24lLhO6gTr1,zMrQmcoQwci9bVVRo6tx,
I/flutter (11506): Does snapshop have data? true
I/flutter (11506): document id: 1579534059562
I/flutter (11506): document id: 1579595374166
I/flutter (11506): Another exception was thrown: FormatException: Invalid number (at character 1)
I/flutter (11506): Does snapshop have data? true
I/flutter (11506): document id: 1579534059562
从控制台,我什至不明白它为什么要从数据库中带来document id: 1579595374166。只有document id: 1579534059562 设置了deliveryTime。数据库有 6 条记录,只有一条设置了交货时间。其他是空的"" 字符串。
因此,几毫秒后,一切都按预期运行,即正确的 UI,只有一个数据库项目显示在屏幕上。当流第二次只返回一个文档时,看起来一切都恢复正常了。事实上,唯一不带红屏的就是控制台长这样的时候:
I/flutter (11506): In the delivered function:XufIsxA8a24lLhO6gTr1,zMrQmcoQwci9bVVRo6tx,
I/flutter (11506): Does snapshop have data? false
I/flutter (11506): Does snapshop have data? true
I/flutter (11506): document id: 1579534059562
这也意味着streamBuilder 正在向列表传递不正确的数据(以及可能的错误来源)。为什么查询有时会返回错误的结果?!
【问题讨论】:
标签: flutter dart google-cloud-firestore formatexception