Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize StateNotifierProvider Asynchronously #127

Closed
coyksdev opened this issue Sep 16, 2020 · 10 comments
Closed

Initialize StateNotifierProvider Asynchronously #127

coyksdev opened this issue Sep 16, 2020 · 10 comments
Labels
documentation Improvements or additions to documentation

Comments

@coyksdev
Copy link
Contributor

coyksdev commented Sep 16, 2020

How can I initialize StateNotifierProvider asynchronously? I want to initialize a list of before creating an instance of it. The data are coming from an api. The problem is the StateNotifierProvider doesn't work in async. Here below is my code:

final countryListProvider = StateNotifierProvider((ref) async {
  try {
    final countryList = await ref.watch(countryRepository).getAllCountries();
    return CountryList(countryList);
  } catch (e) {
    print(e) // also how can i show the error in the UI
  }
});

class CountryList extends StateNotifier<List<CountryEntity>> {
   CountryList([List<Todo> initialCountryList]): super(initialCountryList ?? []);
  
   filterCountries(String str) async {
     try {
       final regex = RegExp(str, caseSensitive: false);
       return state
            .where((element) => regex.hasMatch(element.name))
            .toList();
    } catch(e) {
      rethrow;
    }
   } 
}
@coyksdev coyksdev added the documentation Improvements or additions to documentation label Sep 16, 2020
@rrousselGit
Copy link
Owner

Are you looking for #57?

@coyksdev
Copy link
Contributor Author

coyksdev commented Sep 16, 2020

Hi, thanks for this new state management solution. I think I don't need a FutureProvider because I do not need to listen to it. I will only use country list data to show as a suggestion to my country text field.

@rrousselGit
Copy link
Owner

I was referring to #57 (comment)

@coyksdev
Copy link
Contributor Author

coyksdev commented Sep 16, 2020

oh i see. if i do that where should i call fetch? inside init state? Another thing. What does guard do? Is there a documentation for AsyncValue on how to use it? Thanks!

@rrousselGit
Copy link
Owner

No need for initState. The StateNotifierProvider is enough

For AsyncValue, see its dartdoc https://pub.dev/documentation/riverpod/latest/all/AsyncValue-class.html

@coyksdev
Copy link
Contributor Author

coyksdev commented Sep 17, 2020

Alright. thank you so much! you may close this issue.

@coyksdev
Copy link
Contributor Author

Additional. Can you provide a use case for each Providers in the https://riverpod.dev/? I am overwhelmed because of many Providers. hehehe!

@rrousselGit
Copy link
Owner

Each provider has its own documentation on pub https://pub.dev/documentation/riverpod/latest/all/FutureProvider-class.html

I'll search for a way to make this more accessible

@coyksdev
Copy link
Contributor Author

Thanks Thanks!

@rrousselGit
Copy link
Owner

Closing as duplicate of #57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants