rrousselGit / state_notifier

ValueNotifier, but outside Flutter and with some extra perks
MIT License
311 stars 28 forks source link

Handling empty lists with AsyncValue/.when #66

Closed floodoo closed 2 years ago

floodoo commented 2 years ago

When calling RESTful API's it is quite common to see empty arrays returned instead of null. What is the best solution to handle this in Riverpod with AsyncValue? Currently there is .whenOrNulland .when with data, error and loading. What I am missing is a empty function. As often developers forget to implement this state.

Describe the solution you'd like:

.when(
      data: (data) => print(data),
      empty: () => print('No results found.'),
      loading: () => print('loading'),
      error: (e, st) => print('Error: $e'),
    );

This is what i am doing currently:

.when(
      data: (data) {
        if (data.isEmpty) {
          print('No results found.');
        }

        print(data);
      },
      loading: () => print('loading'),
      error: (e, st) => print('Error: $e'),
    );