Okay I have a FlutterFire
app and using Riverpod
to manage the state
of my app. I have a Listenable Provider
named userProvider
where I get the user data from Cloud Fierestore
. Note that I have a Stream Provider
which updates the userProvider
. Here’s what I mean:
Stream<UserModel> getUserData(String uuid) {
return _users.doc(uuid).snapshots().map(
(event) => UserModel.fromMap(event.data() as Map<String, dynamic>));
}
That above is my Stream Provider and here’s what I mean when saying it updates the state of my userProvider:
void getData(WidgetRef ref, User data) async {
userModel = await ref
.watch(authControllerProvider.notifier)
.getUserData(data.uid)
.first;
ref.read(userProvider.notifier).update((state) => userModel);
}
the above function is in my main.dart file.
My userProvider is like this:
final userProvider = StateProvider<UserModel?>((ref) => null);
okay so I made it nullable because before sign in there’ll be no user signed in meaning no data to hold.
When I try to use the userProvider I’m getting an error saying null check operator used on a null value but I use a null check operator because I’m certain the user is signed in now and there should be data store to that Provider.
Plaese help me know what is wrong with my code.
Thank you
2
Answers
You need to check first for
null
before accessing the data in theProvider
.to check
null
heres the code:Probably the provider state is reset, although I don’t see you have an .autoDispose modifier. The use this way, you can track at what point in time the state of the provider turns into null:
I think it’s a great way to stop provider nulling 🙂 Debugging is everything.