Flutter 如何在颤振中捕获StreamProvider中的错误
我在尝试获取firestore中不存在的数据时遇到了StreamProvider问题Flutter 如何在颤振中捕获StreamProvider中的错误,flutter,dart,Flutter,Dart,我在尝试获取firestore中不存在的数据时遇到了StreamProvider问题 Stream<CollectedItem> streamCollectedItem(String id,int category) { return _db .collection('users') .document(id) .collection('CollectedItems').where('category', isE
Stream<CollectedItem> streamCollectedItem(String id,int category)
{
return _db
.collection('users')
.document(id)
.collection('CollectedItems').where('category', isEqualTo: category)
.limit(1)
.snapshots()
.map((snap) => CollectedItem.fromFirestore(snap.documents.first));
}
streamCollectedItem(字符串id,整数类别)
{
返回_db
.collection('用户')
.文件(id)
.collection('CollectedItems')。其中('category',isEqualTo:category)
.限额(1)
.快照()
.map((snap)=>CollectedItem.fromFirestore(snap.documents.first));
}
我这样调用这个函数:
return StreamProvider<CollectedItem>.value(
value: db.streamCollectedItem(userID,collectMeNotification.category),
child: AlertDialog(
title: Text('Quantité'),
content: SingleChildScrollView(
child: popUp(collectMeNotification,userID)
),
返回StreamProvider.value(
值:db.streamCollectedItem(用户ID、collectMeNotification.category),
子:警报对话框(
标题:文本(“数量”),
内容:SingleChildScrollView(
子项:弹出窗口(collectMeNotification,userID)
),
但是我遇到了一个“没有提供catchError”的问题,因为我正在将我的流与一个不存在的数据链接
如何修复此问题?您可以使用
catchError
在流发出错误时提供回退值
例如,要发出null
,这将是:
StreamProvider(
生成器:()=>someStream,
catchError:(u,u)=>null,
孩子:。。。,
)
看起来您的流正在抛出。这是预期的吗?问题是,当firestore中没有元素时,我的流没有返回null,它正在返回异常。您希望它返回null吗?是的,如果firestore中没有此查询的数据,我希望它返回null。是否可以打印出错误?