【问题标题】:Firebase flutter _CastError (Null check operator used on a null value)Firebase颤动_CastError(用于空值的空检查运算符)
【发布时间】:2022-01-13 09:10:31
【问题描述】:

税务页面上的文件不会出现在屏幕上。我收到以下 _CastError (用于空值的空检查运算符)。有人可以帮忙吗?

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(),
                    ),
                  );
                },
              );
            },
          ),
        ),

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    尝试从getHizmetler 函数返回快照,而不是您当前返回的列表:

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

    调整您的StreamBuilder 喜欢:

    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
        }
      }
    )
    

    【讨论】:

    • 谢谢。该应用程序有效
    • 很高兴听到,不客气。
    猜你喜欢
    • 2022-06-19
    • 1970-01-01
    • 2021-06-13
    • 2022-08-06
    • 2021-12-15
    • 2021-10-22
    • 2021-11-17
    • 2022-01-19
    • 2021-07-08
    相关资源
    最近更新 更多