How to get Flutter DropDownMenu Item from Array created on Firebase

36 Views Asked by At

I am trying to retrieve the arrayList from firebase for my DropDownButton items. In the below screenshot, I am looking for district array item to show as DropDownButton items.

enter image description here

I have written below code that is retrieving the State List from documents but I have to retrieve district array from the field.

class CreateProfile extends StatefulWidget {
  const CreateProfile({Key? key}) : super(key: key);

  @override
  State<CreateProfile> createState() => _CreateProfileState();
}

class _CreateProfileState extends State<CreateProfile> {

  @override
  Widget build(BuildContext context) {
    var selectedState;
    List<DropdownMenuItem> stateItems=[];
    return Column(
      children: [
        Container(
          height: 100,
          width: double.infinity,
          color: Colors.yellow,
        ),
        SizedBox(
          width: double.infinity,
          child: StreamBuilder<QuerySnapshot>(
            stream: FirebaseFirestore.instance.collection('stateList').snapshots(),
            builder: (context, snapshot){
              if(!snapshot.hasData){
                const Text('loading');
              }else{
                int length=snapshot.data!.docs.length;
                for(int i=0;i<length;i++){
                  DocumentSnapshot documentSnapshot=snapshot.data!.docs[i];
                  if(stateItems.length<length){
                    stateItems.add(DropdownMenuItem(value: documentSnapshot.id,child: Text(documentSnapshot.id,
                      style: const TextStyle(color: Colors.red),
                    ),
                    ),
                    );
                  }
                }
              }return Row(
                children: [
                  DropdownButton(
                      items: stateItems,
                      onChanged: (stateValue){
                        setState(() {
                          selectedState=stateValue;
                        });
                      },value: selectedState,)
                ],
              );
            },
          ),
        ),
      ],
    );
  }
}
0

There are 0 best solutions below