Flutter Simulator Error While Having a Dropdown Which takes values from firebase 集合

Flutter Simulator Error While Having a Dropdown Which takes values from firebase collection

谁能帮我解决这个错误。我的应用程序中有一个下拉菜单,它从 firebase 集合中获取菜单项。此错误仅出现在模拟器中,需要有关此的帮助。有时它有效,有时它会出错。任何解决方案。 Android 和 iOS.

都存在错误

这是我的下拉菜单代码,它从 firebase 集合中获取项目

            Container(
        padding: EdgeInsets.all(10.0),
        child: StreamBuilder<QuerySnapshot>(
          stream: FirebaseFirestore.instance.collection("test").snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData)
              const Text("Loading");
            else {
              List<DropdownMenuItem> occupationItems = [];
              for (int i = 0; i < snapshot.data.docs.length; i++) {
                DocumentSnapshot snap = snapshot.data.docs[i];
                occupationItems.add(
                  DropdownMenuItem(
                    child: Text(
                      snap.id,
                      style: TextStyle(color: Colors.black),
                    ),
                    value: "${snap.id}",
                  ),
                );
              }
              return DropdownButtonFormField(
                validator: (value) =>
                    value.isEmpty ? 'Select your occupation' : null,
                decoration: InputDecoration(
                  border: InputBorder.none,
                ),
                icon: FaIcon(
                  FontAwesomeIcons.chevronCircleDown,
                  color: Colors.grey,
                  size: 20.0,
                ),
                items: testItems,
                onChanged: (testValue) {
                  setState(
                    () {
                      test = testValue;
                    },
                  );
                },
                value: test,
                hint: Text(
                  "test",
                  style: TextStyle(
                    color: Colors.grey,
                  ),
                ),
              );
            }
          },
        ),
      ),

请在流构建器中的 if 条件中替换它

if(snapshot.data == null || snapshot.data.docs.length == 0 || snapshot.hasError ){
 return Text('loading');
}

你错过了return:

builder: (context, snapshot) {
        if (!snapshot.hasData)
          const Text("Loading");  //// No return here
        else {
          List<DropdownMenuItem> occupationItems = [];
          for (int i = 0; i < snapshot.data.docs.length; i++) {