River_pod: FutureProvider not working properly.

Created on 3 Sep 2020  路  2Comments  路  Source: rrousselGit/river_pod

To Reproduce
I have futureProvider is

final currentProfileProvider = FutureProvider<Profile>((ref) async {
  final currentMSV = await SharedPrefs.instance.getCurrentMSV();

  return ref.read(profileRepositoryProvider).getUserByMSV(currentMSV);
});

usage:

            final currentProfile = context.read(currentProfileProvider);

            currentProfile.whenData((profile) {
              print(profile.toJson());
              print('data is ${profile.toJson()}');
              showAlertMessage(context, profile.Token);
            });

in the first click: nothing happens.
second click: dialog show with data.
third click: dialog show with data.

ezgif com-video-to-gif

Most helpful comment

whenData does not wait for the future to resolve. It's a synchronous operation, and if the future isn't completed, nothing happens.

What you want is to await the future instead:

final profile = await context.read(currentProfileProvider.future);

print(profile.toJson());
print('data is ${profile.toJson()}');
showAlertMessage(context, profile.Token);

All 2 comments

whenData does not wait for the future to resolve. It's a synchronous operation, and if the future isn't completed, nothing happens.

What you want is to await the future instead:

final profile = await context.read(currentProfileProvider.future);

print(profile.toJson());
print('data is ${profile.toJson()}');
showAlertMessage(context, profile.Token);

It worked. Thank you a lot!

Was this page helpful?
0 / 5 - 0 ratings