Home > Blockchain >  Firebase flutter _CastError (Null check operator used on a null value)
Firebase flutter _CastError (Null check operator used on a null value)

Time:12-09

The documents on the tax page do not appear on the screen. I am getting the following _CastError (Null check operator used on a null value). Is there anyone who can help?

firestore_db_services.dart

 @override
  Stream<List<Vergi>> getHizmetler(String currentUserID) {
    var snapShot = _firebaseFirestoreDB
        .collection("hizmetler")
        .doc(currentUserID)
        .collection("vergi")
        .orderBy("aciklamaDate")
        .snapshots();

    return snapShot.map(
      (vergiListesi) => vergiListesi.docs
          .map(
            (vergi) => Vergi.fromMap(vergi.data()),
          )
          .toList(),
    );
  }

vergi.dart

Expanded(
          child: StreamBuilder<List<Vergi>?>(
            stream: _userModel.getHizmetler(_currentUser.userID),
            builder: (context, snapShot) {
              if (snapShot.hasError) {
                return Center(
                  child: MyIndicator(),
                );
              }
              if (snapShot.connectionState == ConnectionState.waiting) {
                return Text("Yükleniyor...");
              }
              List<Vergi>? tumVergiListesi = snapShot.data;
              return ListView.builder(
                itemCount: tumVergiListesi!.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(
                      tumVergiListesi[index].aciklama.toString(),
                    ),
                    subtitle: Text(
                      tumVergiListesi[index].fiyat.toString(),
                    ),
                  );
                },
              );
            },
          ),
        ),

CodePudding user response:

Try returning snapshots from getHizmetler function and not the list you are currently returning:

Stream<QuerySnapshot>getHizmetler(String currentUserID) {
  return _firebaseFirestoreDB
      .collection("hizmetler")
      .doc(currentUserID)
      .collection("vergi")
      .orderBy("aciklamaDate")
      .snapshots();
}

Adjust your StreamBuilder like:

StreamBuilder<QuerySnapshot>(
  stream: _userModel.getHizmetler(_currentUser.userID),
  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (snapshot.hasError) {
      // error handling
      return ...;
    }
    if (snapshot.connectionState == ConnectionState.waiting) {
      // progress indicator
      return ...;
    }
    if (snapshot.hasData) {
      // here you can do the conversion you like, result will be in:
      // snapshot.data!.docs
    }
  }
)
  • Related