rrousselGit / provider

InheritedWidgets, but simple
https://pub.dev/packages/provider
MIT License
5.11k stars 512 forks source link

Stream issue with StreamProvider #860

Closed mpastewski closed 8 months ago

mpastewski commented 8 months ago

Describe the bug I'm using StreamBuilder to listen to changes in user authentication and then StreamProvider to fetch data from Firestore while constantly listening to changes in user documents. I sometimes got occasional crashes for a small percentage of users, but lately, this number has risen.

This is the crash from Crashlytics: Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: An exception was throw by _MapStream<DocumentSnapshot<Map<String, dynamic>>, UserData> listened by at FirebaseCrashlytics.recordError(firebase_crashlytics.dart:119) at FirebaseCrashlytics.recordFlutterError(firebase_crashlytics.dart:145) at FlutterError.reportError(assertions.dart:1185) at _streamStartListening.<fn>.<fn>(async_provider.dart:32)

To Reproduce ` runApp(

StreamBuilder<User?>(
  stream: FirebaseAuth.instance.authStateChanges(),
  builder: (context, snapshot) {
    return StreamProvider<UserData>.value(
        value: getUserData(snapshot.data?.uid),
        initialData: UserData(),
        child: Phoenix(child: MyApp(snapshot)));
  },
),

);

` Stream getUserData(String? uid) { if (uid != null) {

return FirebaseFirestore.instance
    .collection('users')
    .doc(uid)
    .snapshots()
    .map((doc) {
  final data = doc.data();
  if (data != null) {
    return UserData.fromJson(data);
  } else {
    return UserData();
  }
});

} return Stream.value(UserData()); }`

Expected behavior I want to use:

final data = Provider.of<UserData>(context, listen: false);

In the entire app get the newest version of UserData while still being sure that the user is authenticated.

rrousselGit commented 8 months ago

How is this related to Provider? To me it sounds like your stream is emitting an exception. Provider isn't responsible for this

mpastewski commented 8 months ago

Thanks Remi for this hint that I should seek more on the Stream side and sorry for opening this issue in Provider. How would you troubleshoot it from there?

rrousselGit commented 8 months ago

I don't think there's a clear way for me to help. But I'd suggest asking on forums, like Discord