skip to Main Content

I have been using Notifier with void type for the state. I am wondering is this the recommended way to use notifier or should I just use the normal Provider? I used this just to access some function.

2

Answers


  1. It makes no sense to use a Notifier with type void. It is assumed that:

    • Provider for a simple state that we cannot change externally
    • StateProvider is also for a simple state, but which we can change externally
    • (Async)NotifierProvider for complex state when you need to have methods to manage it

    But one way or another, some state must be stored in each of the providers. That’s the point of it) If there is no state – maybe it’s just a utility class or a repository

    Verdict: it makes no sense to use providers with void type state.

    Login or Signup to reply.
  2. Whether it is a Provider or a Notifier, neither should return void.

    Returning void is generally a sign that something is going wrong. There are some use-cases (such as when returning a Stream<void> for periodic refresh of another provider). But generally, you do not want to do so.

    From my experience, most people trying to return void in a provider only do so because they have split their app into too many small chunks.

    A Notifier is meant to be a full CRUD.

    You’d meant to do:

    class UserNotifier extends AsyncNotifier<User> {
      @override
      Future<User> build() => http.get('/user');
    
      setUser(User user) => http.post('/user', user.toJson());
    
      deleteUser(String userId) => http.delete('/user', userId);
    }
    

    Returning void in build means that chances are your http.get is placed in a different provider.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search