Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In snapshot has no data condition return is missing

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading