簡體   English   中英

Flutter Firebase 查詢 Firestore 和 Streambuilder() 的問題

[英]Flutter Firebase Problems with Query Firestore and Streambuilder()

我正在嘗試搜索與 TextField 中的文本匹配的文檔並使用 Streambuilder 顯示它們。 由於我是 flutter 初學者,我沒有找到有關如何做到這一點的有用視頻和教程。

我正在嘗試做這樣的事情:

Widget body(BuildContext context) {
    if(textControllerSearch.text != "") {
      if(textFocusNodeSearch.hasFocus == false) {
        return ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(15)),
          child: StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore.instance
                  .collection('users')
                  .doc(firebaseUser!.uid.toString())
                  .collection("recipes")
                  .where("recipeName", arrayContains: textControllerSearch.text.trim())
                  .limit(3)
                  .snapshots(),
            builder: (context, snapshot) {
              return ListView.builder(
                  itemCount: snapshot.data!.docs.length,
                  itemBuilder: (context, index) {
                    DocumentSnapshot documentSnapshot = snapshot.data!.docs[index];
                    return ListTile(title: Text(documentSnapshot['recipeName']));
                  });
            },
          ),
        );
      } else {
        return Container(
          alignment: Alignment.topCenter,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                child: Lottie.asset("assets/lottie/search_bubble.json", width: MediaQuery.of(context).size.width * 0.5, fit: BoxFit.fitWidth),
              ),
              Container(
                child: Text(
                  "Searching...",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Color(0xFFEC8E00).withOpacity(0.5),
                    fontSize: 10,
                  ),
                ),
              ),
            ],
          ),
        );
      }
    } else {
      return Container();
    }
  }

文本字段:

TextFormField(
                                      focusNode: textFocusNodeSearch,
                                      textAlign: TextAlign.left,
                                      keyboardType: TextInputType.name,
                                      controller: textControllerSearch,
                                      autofocus: true,
                                      onFieldSubmitted: (value) {
                                        setState(() {});
                                      },
                                      onChanged: (value) {
                                        if(textControllerSearch.text.trim() == "") {
                                          setState(() {
                                            notSearching = true;
                                          });
                                        } else {
                                          setState(() {
                                            notSearching = false;
                                          });
                                        }
                                      },
                                      style: TextStyle(
                                        fontFamily: 'Font',
                                        fontSize: 16,
                                        color: Colors.white.withOpacity(0.75),
                                      ),
                                      obscureText: false,
                                      cursorColor: Color(0xFFEC8E00),
                                      decoration: InputDecoration(
                                        border: InputBorder.none,
                                        hintText: 'Search',
                                        hintStyle: TextStyle(
                                          fontSize: 14,
                                          color: Colors.white.withOpacity(0.75),
                                          fontFamily: 'Font',
                                        ),
                                      ),
                                    ),

我收到此錯誤,但我無法清楚地理解錯誤是什么以及導致問題的原因:

The following _CastError was thrown building StreamBuilder<QuerySnapshot<Object?>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Object?>, AsyncSnapshot<QuerySnapshot<Object?>>>#6bf78):
Null check operator used on a null value

這是因為某處有一個 null 值。 嘗試更換

 return ListView.builder(
                  itemCount: snapshot.data!.docs.length,
                  itemBuilder: (context, index) {
                    DocumentSnapshot documentSnapshot = snapshot.data!.docs[index];
                    return ListTile(title: Text(documentSnapshot['recipeName']));
                  });

使用snapshot.data?.docs[index];

根據您的日志,錯誤出在此處:

 child: StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore.instance
                  .collection('users')
                  .doc(firebaseUser!.uid.toString())
                  .collection("recipes")
                  .where("recipeName", arrayContains: textControllerSearch.text.trim())
                  .limit(3)
                  .snapshots(),
            builder: (context, snapshot) {
              return ListView.builder(
                  itemCount: snapshot.data!.docs.length,
                  itemBuilder: (context, index) {
                    DocumentSnapshot documentSnapshot = snapshot.data!.docs[index];
                    return ListTile(title: Text(documentSnapshot['recipeName']));
                  });

你設置。 關於快照,數據。 檢查 snapshot.data 是否不同於 null。

child: StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore.instance
                  .collection('users')
                  .doc(firebaseUser!.uid.toString())
                  .collection("recipes")
                  .where("recipeName", arrayContains: textControllerSearch.text.toString().trim()) //add toString()
                  .limit(3)
                  .snapshots(),
            builder: (context, snapshot) {
              return ListView.builder(
                  itemCount: snapshot.data!.docs.length,
                  itemBuilder: (context, index) {
                    DocumentSnapshot documentSnapshot = snapshot.data!.docs[index];
                    return ListTile(title: Text(documentSnapshot['recipeName'].toString())); //add toString()
                  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM