skip to Main Content

As the question implies, I am trying to create an AutoDisposeAsyncNotifierProviderFamily. I currently have this:

final participantProvider = AsyncNotifierProvider.autoDispose.family<
    ParticipantNotifier,
    Map<String, List<String>>,
    String
  >((ref, id) => ParticipantNotifier(id));

class ParticipantNotifier extends AsyncNotifier<Map<String, List<String>>> {
  ParticipantNotifier(this.id);
  final String id;

  @override
  FutureOr<Map<String, List<String>>> build() {
    // Get some participants for this event's id
  }
}

(extra indents and linebreaks added for readability). However, I get the error: The argument type 'ParticipantNotifier Function(dynamic, dynamic)' can't be assigned to the parameter type 'ParticipantNotifier Function()'. I don’t get why the parameter has type ParticipantNotifier Function() even if I add the family modifier. With some other simpler providers I successfully created Families with a similar syntax. Any help much appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    I found the answer: the Notifier itself has to be defined as

    class ParticipantNotifier
        extends AutoDisposeFamilyAsyncNotifier<Map<String, List<String>>, String> {
    
    

    So it needs to extend AutoDisposeFamilyAsyncNotifier


  2. Following OP’s answer (as well as this answer by Remi), we’ll also need to remove the constructor and add the input to the build method.

    Here’s the full code for this example:

    final participantProvider = AutoDisposeAsyncNotifierProviderFamily<
      ParticipantNotifier,
      Map<String, List<String>>,
      String>(ParticipantNotifier.new);
    
    class ParticipantNotifier
        extends AutoDisposeFamilyAsyncNotifier<Map<String, List<String>>, String> {
    
      @override
      FutureOr<Map<String, List<String>>> build(String arg) {
        // Get some participants for this event's id
        return {'id': [arg]};
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search