gpt4 book ai didi

flutter - 抛出了另一个异常 : FormatException: Invalid number (at character 1)

转载 作者:行者123 更新时间:2023-12-03 02:44:13 27 4
gpt4 key购买 nike

为什么会报错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 条记录,只有一条记录设置了 deliveryTime。其他都是空的 ""字符串。

因此,几毫秒后,一切都按预期工作,即正确的 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正在向列表传递不正确的数据(以及可能的错误来源)。为什么查询有时会返回错误的结果?!

最佳答案

它再次发生,现在我知道为什么了。在代码中,它实际上是在这一行 int.parse(item['deliveryTime']) 中的问题因为在 parse()方法 如果输入字符串不是有效的整数形式,程序将抛出 FormatException:
所以为了处理这个案子,

int.tryParse(item['deliveryTime']) ?? defaultValue;
你也可以使用 Dart try-catch 块:
try {
var n = int.parse(item['deliveryTime']);
print(n);
} on FormatException {
print('Format error!');
}
// Format error!
int类 parse()方法也给了我们处理 FormatException 的方法带有 onError 参数的情况。
var num4 = int.parse(item['deliveryTime'], onError: (source) => -1);
// -1
抛出异常时, onError将使用 source 作为输入字符串调用。现在我们可以返回一个整数值或空值……在上面的例子中,函数返回 -1每当 source在错误的整数文字中。

关于flutter - 抛出了另一个异常 : FormatException: Invalid number (at character 1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59853750/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com