Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to make dependent multilevel DropDown in flutter?

Posted on November 11, 2022 By

Solution 1 :

You have to make cityModel = null in onChanged callback of State dropdown.

setState(() {
  cityModel = null;
  stateModel = selectedState;
  _city = _fetchCities(stateModel.billstateid);
});

There should be exactly one item with [DropdownButton]’s value:
Instance of ‘City’. Either zero or 2 or more [DropdownMenuItem]s were
detected with the same value

This error occurs here, because the value you are passing not in the items of DropdownButtonFormField(city dropdown).

When you select a State, you are fetching new list of city list and passing it to CityDropDown but forgot to clear the previously selected city(cityModel).

You can also refer this example: DartPad

Solution 2 :

I am also facing a problem until new state data is not fetched It is showing previous state data. The approach I have used is different. I am not using future Builders.
Here is My code:

               Container(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      new Expanded(
                        child: new Container(
                          width: 450,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                "Source",
                                style: TextStyle(
                                    fontSize: 15, fontWeight: FontWeight.bold),
                              ),
                              source1 != null ? DropdownButtonFormField<String>(
                                isExpanded: true,
                                validator: (value) => value == null ? 'field required' : null,
                                hint: Text("Select Source"),
                                items: source1.data.map((item) {
                                  // print("Item : $item");
                                  return DropdownMenuItem<String>(
                                    value: item.descr,
                                    child: Text(item.descr),
                                  );
                                }).toList(),
                                onChanged: (String cat) {
                                  setState(() {
                                    subsourseStr = null;
                                    sourceStr = cat;
                                    getSubSource2(sourceStr);
                                  });
                                },
                                value: sourceStr,
                              ):SizedBox(height: 10),
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
//
                Container(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      new Expanded(
                        child: new Container(
                          width: 450,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                "Sub Source",
                                style: TextStyle(
                                    fontSize: 15, fontWeight: FontWeight.bold),
                              ),
                              subSource2 != null ? DropdownButtonFormField<String>(
                                isExpanded: true,
                                validator: (value) => value == null ? 'field required' : null,
                                hint: Text("Select Sub Source"),
                                items: subSource2.data.map((item) {
                                  return DropdownMenuItem<String>(
                                    value: item.descr,
                                    child: Text(item.descr),
                                  );
                                }).toList(),
                                onChanged: (String cat) {
                                  setState(() {
                                    subsourseStr = cat;
                                  });
                                },
                                value: subsourseStr,
                              ):SizedBox(height: 10,),
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),

Problem :

I am trying to make dependent multilevel dropdown first contains states list and second contains cities list, all the data fetched from API. Initially, I load state dropdown, when I select the state then cities of that state load if I select city, the city selected successfully but when I change the state value then an error occurs. What is the right way to reload the second dropdown if changes will make in the first dropdown?

READ  [FIXED] java - android - Comparing two very large ArrayLists one of them retrieved from firebase
Powered by Inline Related Posts

Error: There should be exactly one item with [DropdownButton]’s value: Instance of ‘City’.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value

Future _state;
Future _city;

@override
  void initState() {
    super.initState();
    _state = _fetchStates();
  }
Future<List<StateModel>> _fetchStates() async {
    final String stateApi = "https://dummyurl/state.php";
    var response = await http.get(stateApi);

    if (response.statusCode == 200) {
      final items = json.decode(response.body).cast<Map<String, dynamic>>();

      List<StateModel> listOfUsers = items.map<StateModel>((json) {
        return StateModel.fromJson(json);
      }).toList();

      return listOfUsers;
    } else {
      throw Exception('Failed to load internet');
    }
  }
  Future<List<City>> _fetchCities(String id) async {
    final String cityApi = "https://dummyurl/city.php?stateid=$id";
    var response = await http.get(cityApi);

    if (response.statusCode == 200) {
      final items = json.decode(response.body).cast<Map<String, dynamic>>();
      print(items);
      List<City> listOfUsers = items.map<City>((json) {
        return City.fromJson(json);
      }).toList();

      return listOfUsers;
    } else {
      throw Exception('Failed to load internet');
    }
  }

State Dropdown

FutureBuilder<List<StateModel>>(
                                        future: _state,
                                        builder: (BuildContext context,
                                            AsyncSnapshot<List<StateModel>> snapshot) {
                                          if (!snapshot.hasData)
                                            return CupertinoActivityIndicator(animating: true,);
                                          return DropdownButtonFormField<StateModel>(
                                            isDense: true,
                                            decoration: spinnerDecoration('Select your State'),
                                            items: snapshot.data
                                                .map((countyState) => DropdownMenuItem<StateModel>(
                                              child: Text(countyState.billstate),
                                              value: countyState,
                                            ))
                                                .toList(),
                                            onChanged:(StateModel selectedState) {
                                              setState(() {
                                                stateModel = selectedState;
                                                _city = _fetchCities(stateModel.billstateid);
                                              });
                                            },
                                            value: stateModel,
                                          );
                                        }),

City Dropdown

FutureBuilder<List<City>>(
                                        future: _city,
                                        builder: (BuildContext context,
                                            AsyncSnapshot<List<City>> snapshot) {
                                          if (!snapshot.hasData)
                                            return CupertinoActivityIndicator(animating: true,);
                                          return DropdownButtonFormField<City>(
                                            isDense: true,
                                            decoration: spinnerDecoration('Select your City'),
                                            items: snapshot.data
                                                .map((countyState) => DropdownMenuItem<City>(
                                              child: Text(countyState.billcity)
                                                .toList(),
                                            onChanged: (City selectedValue) {
                                              setState(() {
                                                cityModel = selectedValue;
                                              });
                                            },
                                            value: cityModel,
                                          );
                                        }),
class StateModel {
  String billstateid;
  String billstate;
  String billcountryid;

  StateModel({this.billstateid, this.billstate, this.billcountryid});

  StateModel.fromJson(Map<String, dynamic> json) {
    billstateid = json['billstateid'];
    billstate = json['billstate'];
    billcountryid = json['billcountryid'];
  }
}
class City {
  String billcityid;
  String billcity;
  String billstateid;

  City({this.billcityid, this.billcity, this.billstateid});

  City.fromJson(Map<String, dynamic> json) {
    billcityid = json['billcityid'];
    billcity = json['billcity'];
    billstateid = json['billstateid'];
  }

Comments

Comment posted by Jorden

Thank you so much it is working, I want to know is this is the correct way which I follow when working with dependent dropdown

READ  [FIXED] Unsure of the syntax when creating a instance of a class that inherits Application (Android studio kotlin)
Powered by Inline Related Posts

Comment posted by Crazy Lazy Cat

@Toyed If you want to show the loading indicator in place of

Comment posted by Jorden

Ok..according to this way which I follow I am facing a problem until new state data is not fetched It is showing previous state data.

Comment posted by Crazy Lazy Cat

@Toyed Its because you are using

Comment posted by Jorden

Yes, Now Working Fine, You are so genius. Thanks a lott

Android Tags:android, dart, flutter, ios

Post navigation

Previous Post: Can a beginner learn Moonlight Sonata 1st Movement?
Next Post: [FIXED] java – App keeps crashing when ListView item is clicked on

Related Posts

[FIXED] Why is DefaultLifecycleObserver preferred over the Lifecycle annotations when Java 8 on Android is available? Android
[FIXED] java – How can I add space in the RecyclerView? Android
[FIXED] xamarin.forms – Syncfusion is causing build errors after migrating to AndroidX on Xamarin Forms Android
[FIXED] android 12 – enable eSIM in samsung s21 FE? Android
[FIXED] java – App crash crash if button clicked at start but not if clicked after Seekbar moved Android
[FIXED] java – How can I put a listView inside a scroll view on android studio? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme