简体   繁体   English

Flutter - 如何从 static 列表中进行动态下拉?

[英]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.我有一个名为 getBreed() 的方法,根据填充在另一个小部件中的物种,返回狗或猫品种列表以填充品种的动态下拉列表。 But this method is not working well, an error occurs:但是这个方法效果不好,出现错误:

The method 'map' was called on null.在 null 上调用了方法“map”。 Tried calling: map > (Closure: (String) => DropdownMenuItem )尝试调用:map >(关闭:(字符串)=> 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();
    }
  }
}

The word return is missing in the "getBreed" method “getBreed”方法中缺少return这个词

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM