如何在颤振中捕获 StreamProvider 中的错误

Nac*_*izi 3 dart flutter

当我尝试在 firestore 中获取不存在的数据时,我遇到了 StreamProvider 的问题。

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));

    }
Run Code Online (Sandbox Code Playgroud)

我像这样调用这个函数:

return StreamProvider<CollectedItem>.value(
     value: db.streamCollectedItem(userID,collectMeNotification.category),
          child: AlertDialog(
            title: Text('Quantité'),
            content: SingleChildScrollView(
              child: popUp(collectMeNotification,userID)

              ),
Run Code Online (Sandbox Code Playgroud)

但是我遇到了“没有提供 catchError”的问题,因为我正在将我的流与不存在的数据链接起来。

我怎样才能解决这个问题 ?

Rém*_*let 9

您可以使用catchError提供回退值,以防流发出错误。

以发射null为例,这将是:

StreamProvider(
  builder: (_) => someStream,
  catchError: (_, __) => null,
  child: ...,
)
Run Code Online (Sandbox Code Playgroud)