簡體   English   中英

主體可能正常完成,導致返回“null”,但返回類型“Widget”可能是不可為 null 的類型。 Flutter碼

[英]The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type. Flutter code

我一直在努力在 flutter 中顯示一個下拉按鈕,該按鈕從雲 Firestore 集合中提取數據,但最終出現錯誤。 任何反饋將不勝感激。 以下是我正在處理的代碼片段:

child: Column(
          children: <Widget>[
            const SizedBox(
              height: 10,
            ),
            StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore.instance.collection('cars').snapshots(),
              builder: (context, snapshot)
              {
                if (!snapshot.hasData) {
                  const Text("Loading");
                } else {
                  List<DropdownMenuItem> carItems = [];
                  for (int i = 0; i < snapshot.data!.docs.length; i++) {
                    DocumentSnapshot snap = snapshot.data!.docs[i];
                    carItems.add(
                      DropdownMenuItem(
                        value: "${snap.id}",
                        child: Text(
                          snap.id,
                          style: const TextStyle(color: Colors.deepOrange),
                        ),
                      ),
                    );
                  }
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      const Icon(
                        Icons.car_repair,
                        size: 25.0,
                      ),
                      const SizedBox(
                        width: 50.0,
                      ),
                      DropdownButton<dynamic>(
                        items: carItems,
                        onChanged: (carValue) {
                          final snackBar = SnackBar(
                            content: Text(
                              'Selected Car is $carValue',
                              style: const TextStyle(color: Colors.grey),
                            ),
                          );
                          Scaffold.of(context).showSnackBar(snackBar);
                          setState(() {
                            selectedCar = carValue;
                          });
                        },
                        value: selectedCar,
                        isExpanded: false,
                        hint: const Text(
                          "Choose Car Make",
                          style: TextStyle(color: Colors.deepOrange),
                        ),
                      ),
                    ],
                  );
                }
              }

在快照中沒有數據條件返回丟失

if (!snapshot.hasData) {
          return Text("Loading");
      }

暫無
暫無

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

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