StreamBuilder <QuerySnapshot>(脏,状态:_StreamBuilderBaseState <QuerySnapshot,AsyncSnapshot <QuerySnapshot>):

时间:2020-08-30 21:25:32

标签: flutter google-cloud-firestore

我无法从Firestore检索数据并出现以下错误,

════════小部件库捕获到异常 ══════════════════════════════════════════════════ following以下 断言是在构建StreamBuilder(dirty, 状态:_StreamBuilderBaseState #e568b):构建函数返回null。

有问题的小部件是:StreamBuilder Build函数 绝对不能返回null。

返回将导致建筑小部件填充的空白区域 可用空间,返回“ Container()”。要返回一个空白 占用尽可能少的空间,返回“ Container(width:0.0, 高度:0.0)”。

相关的引起错误的小部件是:StreamBuilder 文件:... dart:140:15引发异常时,这是堆栈:

#0 debugWidgetBuilderValue。 (软件包:flutter / src / widgets / debug.dart:300:7) #1 _Closure.call(dart:core-patch / function.dart) #2 debugWidgetBuilderValue(软件包:flutter / src / widgets / debug.dart:321:4) #3 ComponentElement.performRebuild(package:flutter / src / widgets / framework.dart:4569:7) #4 StatefulElement.performRebuild(package:flutter / src / widgets / framework.dart:4737:11)...


下面是我的代码。

StreamBuilder<QuerySnapshot>(
                  stream: Firestore.instance.collection("currency").snapshots(),
                  builder: (context, snapshot) {
                  if (!snapshot.hasData){
                      print('test pharse');
                       Text("Loading.....");}
                    else {
                      List<DropdownMenuItem> currencyItems = [];
                      for (int i = 0; i < snapshot.data.documents.length; i++) {
                        DocumentSnapshot snap = snapshot.data.documents[i];
                        currencyItems.add(
                          DropdownMenuItem(
                            child: Text(
                              snap.documentID,
                              style: TextStyle(color: Color(0xff11b719)),
                            ),
                            value: "${snap.documentID}",
                          ),
                        );
                      }
                      return Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Icon(Icons.mail,
                              size: 25.0, color: Color(0xff11b719)),
                          SizedBox(width: 50.0),
                          DropdownButton(
                            items: currencyItems,
                            onChanged: (currencyValue) {
                              final snackBar = SnackBar(
                                content: Text(
                                  'Selected Currency value is $currencyValue',
                                  style: TextStyle(color: Color(0xff11b719)),
                                ),
                              );
                              Scaffold.of(context).showSnackBar(snackBar);
                              setState(() {
                                selectedCurrency = currencyValue;
                              });
                            },
                            value: selectedCurrency,
                            isExpanded: false,
                            hint: new Text(
                              "Choose Currency Type",
                              style: TextStyle(color: Color(0xff11b719)),
                            ),
                          ),
                        ],
                      );
                    }
                  }),

1 个答案:

答案 0 :(得分:1)

您需要在return的{​​{1}}部分的Text小部件之前添加!snapshot.hasData

StreamBuilder