riverpod icon indicating copy to clipboard operation
riverpod copied to clipboard

Stream Deprecation Alternative For Use Case

Open mcmah309 opened this issue 6 months ago • 7 comments

StreamProviders stream field is deprecated and planned to be removed in 3.0.0. We currently have something like this. What is the alternative for combining stream providers?

final realtimeObeliskDocumentProvider = StreamProvider.autoDispose<ObeliskDocument>((ref) {
  ObeliskDocument obeliskDocument = ref.watch(obeliskDocumentProvider);
  DocumentInfo originalInfo = obeliskDocument.info;
  if (originalInfo.collectionPath == null || originalInfo.id == null) {
    Logging.e("Document should always have an path and id for: $obeliskDocument");
    return Stream.value(obeliskDocument);
  }
  final String collectionPath = originalInfo.collectionPath!;
  final String docId = originalInfo.id!;
  return ref.watch(realtimeDocumentInfoProvider((collectionPath, docId)).stream).map((info) {
    if (info != null) {
      obeliskDocument.info = info;
    }
    return obeliskDocument;
  });
});

final realtimeDocumentInfoProvider = StreamProvider.autoDispose.family<DocumentInfo?, (String, String)>((ref, path) {
  return FirebaseFirestore.instance.collection(path.$1).doc(path.$2).snapshots().asyncMap((snapshot) async {
    DocumentInfo? info = await snapshot.toFirestoreEntity(DocumentInfo.fromJson);
    if (info == null) {
      Logging.e("Was unable to get document info for collection ${path.$1} with id ${path.$2}");
    }
    return info;
  });
});

Describe the solution you'd like A solution that is ergonomic without stream or removing the deprecation on stream.

Describe alternatives you've considered listen doesn't seem like the best solution for this use case. Switching the first provider to Future and using .future instead of .stream will cause the screen to go to the loading widget callback. Adding skipLoadingOnReload: true to the .when statement in the widget fixes this, but it causes all user interactions on screen to disappear immediately. With the original .stream user interactions such as selections do not disappear until the new widget is ready, which is desired.

mcmah309 avatar Dec 08 '23 21:12 mcmah309