Ekhlass T
Ekhlass T

Reputation: 63

no such method in flutter, the method add was called on null

 FirebaseFirestore.instance
          .collection('women')
          .snapshots()
          .map((snapshot) => snapshot.docs
              .map((document) => CategoryAvatar.fromJson({...document.data()}))
              .toList())
          .listen((avatar) {
        avatarsInfo.addAll(avatar);

when I started to debug, this exception occurred in the IDE at the third line of the code shown above :

Exception has occurred. NoSuchMethodError (NoSuchMethodError: The method 'add' was called on null. Receiver: null Tried calling: add(Instance of 'MethodChannelQuerySnapshot'))

also when I was debugging, the debugger always does pass the fifth and sixth lines for some weird reason, I mean those lines can't be executed by the program.

when I run the code, the text below is displayed on the debugging console

D/HwCustConnectivityManagerImpl( 8132): isBlockNetworkRequestByNonAis, INVALID_SUBSCRIPTION_ID D/ConnectivityManager( 8132): requestNetwork and the calling app is: com.sincerity.sandra W/DynamiteModule( 8132): Local module descriptor class for providerinstaller not found. I/DynamiteModule( 8132): Considering local module providerinstaller:0 and remote module providerinstaller:0 W/ProviderInstaller( 8132): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0. D/HwCustConnectivityManagerImpl( 8132): isBlockNetworkRequestByNonAis, INVALID_SUBSCRIPTION_ID

Upvotes: 1

Views: 2227

Answers (2)

Ekhlass T
Ekhlass T

Reputation: 63

the problem has been solved by adding the await keyword at the beginning of the query

     await _firebaseFirestore.collection('women').get().then(
        (QuerySnapshot querySnapshot) {
          querySnapshot.docs.forEach(
            (doc) {
              avatarsInfo.add(
                CategoryAvatar.fromJson(
                  {
                    ...doc.data(),
                  },
                ),
              );
            },
          );
        },
      );

Upvotes: 1

Raine Dale Holgado
Raine Dale Holgado

Reputation: 3460

Try this is it works but it only retrieves the data one cycle.

  FirebaseFirestore.instance.collection('women').snapshots().map((snapshot) {
      snapshot.documents.forEach((snapshot) {
        print(snapshot.data);
        CategoryAvatar avatar = CategoryAvatar.fromJson(snapshot.data());
        avatarsInfo.addAll(avatar);
      });
    }).toList();

If your streaming the data try this

  Stream<List<CategoryAvatar>> avatarList() {
    return FirebaseFirestore.instance.collection('women').
        .snapshots()
        .map((value) => value.documents
            .map((result) => CategoryAvatar.fromJson(result.data()))
            .toList());
  }

i hope i helped you

Upvotes: 0

Related Questions