I have an app where I use StreamProvider to fetch data from Firestore & pass it around in the app. While it works as I want, the problem arises when I log out & sign in again from a different user ID, it still shows the data from the first user. What do I do to make sure the StreamProvider disposes the current value when a user logs out, and refetches the proper user data when a new user signs in? Here's my code
main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final db = Database();
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<AuthenticationService>(
create: (_) => AuthenticationService(FirebaseAuth.instance),
),
StreamProvider(
// key: ObjectKey(FirebaseAuth.instance.currentUser.uid),
create: (context) =>
context.read<AuthenticationService>().authStateChanges,
),
ChangeNotifierProvider<AppointmentProvider>(
create: (_) => AppointmentProvider(),
),
StreamProvider(
catchError: (context, error) {
print(error);
},
create: (BuildContext context) => db.getAppointments(),
),
],
child: MaterialApp(
key: GlobalObjectKey(context),
debugShowCheckedModeBanner: false,
title: 'Hospital App',
theme: appTheme,
home: AuthenticationWrapper(),
),
);
}
}
db.getAppointments() method
Stream<List<AppointmentModel>> getAppointments() {
return _firebaseFirestore
.collection("DoctorAppointments")
.doc(uid)
.collection("DoctorAppointmentsList")
.snapshots()
.map((snapShot) => snapShot.docs
.map((document) => AppointmentModel.fromJson(document.data()))
.toList());
}