Liam Park
Liam Park

Reputation: 511

Flutter - How to make dynamic dropdown from static list?

I have a method called getBreed() that depending of the species filled in another widget, returns the list of dog or cat breeds to populate a dynamic dropdown of breeds. But this method is not working well, an error occurs:

The method 'map' was called on null. Tried calling: map > (Closure: (String) => DropdownMenuItem )

However, if I call the method that returns the list directly it works, example:

//instead of this
DropdownContent.getBreed(widget.pet.specie).map<DropdownMenuItem<String>>((String value) {
//i put this
DropdownContent.listOfDogBreeds().map<DropdownMenuItem<String>>((String value) {

Why is this happening? Bellow is my full code:

new Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new DropdownButton<String>(
              value: widget.pet.breed == null
                  ? dropdownInitialValue
                  : widget.pet.breed,
              icon: Icon(Icons.arrow_downward),
              iconSize: 15,
              elevation: 16,
              style: TextStyle(color: Colors.black, fontSize: 14),
              underline: Container(
                height: 2,
                color: Colors.grey,
              ),
              onChanged: (String newValue) {
                setState(() {
                  dropdownInitialValue = newValue;
                  widget.pet.breed = newValue;
                });
              },
              items: DropdownContent.getBreed(widget.pet.specie).map<DropdownMenuItem<String>>((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
            )
          ])



class DropdownContent {
  static List<String> listOfDogBreeds() {
    return [
      'Select',
      'Australian Cattle',
      'Basset Hound',    
      'Chihuahua',
      'Chow Chow'      
    ];
  }

  static List<String> listOfCatBreeds() {
    return <String>[
      'Select',     
      'American Shorthair',     
      'Bengal',
      'Maine Coon',
      'Sphynx'
    ];
  }


  static getBreed(String specie) {
    if (specie.contains('dog')) {
      listOfDogBreeds();
    }
    if (specie.contains('cat')) {
      listOfCatBreeds();
    }
  }
}

Upvotes: 0

Views: 278

Answers (1)

farouk osama
farouk osama

Reputation: 2529

The word return is missing in the "getBreed" method

Upvotes: 1

Related Questions