Flutter - How to get Map data of an Array inside Firestore as Dropdownlist

Viewed 52

I'm new in Flutter with Firebase. I'm trying to get all the name from array in Firestore as Dropdownlist.But I can't be able to get all the data in Dropdownlist. Can someone help me out to solve this problem? Thanks.

Firebase collection Image:

Firebase collection Image

The whole sample data;

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class DropDown extends StatefulWidget {

@override
_DropDownState createState() => _DropDownState();
}

class _DropDownState extends State<DropDown> {
 var Storagelocation;
 var setDefault = true;

  @override
  void initState() {
  super.initState();
   }


  @override
   Widget build(BuildContext context) {

 return Scaffold(
        appBar: AppBar(title:Center(child: Text('Locations'))),
                body: Container(
                                        height: 185,
                                        child: Column(children: [
                                          Text('Main Location'),
                                        StreamBuilder<QuerySnapshot>(
                                        stream: FirebaseFirestore.instance
                                            .collection('location')
                                            .snapshots(),
                                        builder: (BuildContext context,
                                            AsyncSnapshot<QuerySnapshot> snapshot) {
                                          if (!snapshot.hasData) return Container();
                                          if (setDefault) {
                                            Storagelocation = snapshot.data.docs[0]['Location'][0]['name'];
                                            debugPrint('setDefault: $Storagelocation');
                                          }

                                          return DropdownButton(
                                            isExpanded: false,
                                            value: Storagelocation,
                                            items: snapshot.data.docs.map((DocumentSnapshot document) {

                                              return document['Location'].map((e){
                                                return DropdownMenuItem(
                                                  value: e['name'],
                                                  child: Text(e['name']),
                                                );
                                              });

                                            }).expand((element) => element).toList(),
                                              onChanged: (data) {
                                              setState(
                                                    () {
                                                  Storagelocation =data;
                                                },
                                              );
                                            },
                                          );
                                        },
                                      ),

                                      ]),
   ),
                              );

   }
   }

All I'm getting is type 'List dynamic ' is not a subtype of type 'List<DropdownMenuItem dynamic ?' at the place of Dropdownmenu.

1 Answers

Try updated answer


 snapshot.data.docs.map((DocumentSnapshot document) {

 return document['Location'].map((e){
    return DropdownMenuItem(
      value: e['name'],
      child: Text(e['name']),
      );
  });
  
  }).expand((element) => element).toList()

Comment out after try..!

Related