Home > Mobile >  The body might complete normally, causing 'null' to be returned, but the return type, 
The body might complete normally, causing 'null' to be returned, but the return type, 

Time:08-16

I have been working on displaying a dropdown button in flutter that draws its data from a cloud Firestore collection but I end up getting an error. Any feedback will be highly appreciated. Below is the code snippet on what I am working on:

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

CodePudding user response:

In snapshot has no data condition return is missing

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