类型转换下拉按钮中的问题

时间:2019-05-09 07:01:18

标签: flutter

这是我的代码

String url="Http://77.68.73.59/API/country/allCountries";

   List<String> data = List();
   var dt;
   var vari;

  @override
  void init State() {
  superstate();
  this.getJosnData();
 }

Future<String>getJosnData()async{
var response=await http.get(Uri.encodeFull(url),headers: {"Accept": "application/json"});
var convertDataToJson= json.decode(response.body);
setState((){
data=convertDataToJson;
dt=data;
print("My_Country : $dt");
vari=dt;
print("Response Data: $vari");
return "Success";

});

   body: SingleChildScrollView(
     child: Stack(
       children: <Widget>[
         new Container(     
          padding: EdgeInsets.all(30.0),
           child: Column(
          misalignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            DropdownButton<dynamic>(
            hint: Text('Please choose a location'),  
            value: _selectedLocation,
            onChanged: (newValue) {
            setState(() {
            _selectedLocation = newValue;
           });
         },
            items: dt.map((location) {
            var rowtext = location['text'];
             return DropdownMenuItem<dynamic>(
             child: new Text(rowtext),
             value: rowtext,
           );
        }).toList(),
      ),

我的API响应方式:

Response Data: [{value: 1, text: Afghanistan}, {value: 2, text: Albania}, {value: 3, text: Algeria}, 
{value: 4, text: American Samoa}, {value: 5, text: Andorra}, {value: 6, text: Angola}, {value: 7, text: Anguilla},
 {value: 8, text: Antarctica}, {value: 9, text: Antigua and Barbuda}, {value: 10, text: Argentina}, {value: 11, text: Armenia},
 {value: 17, text: Bahrain}, {value: 242, text: Bangladesh}, {value: 19, text: Barbados},

但未在Dropdown中设置,请提供错误:列表'dynamic'不是类型为'DropdownMenuItem dynamic'的子类型

0 个答案:

没有答案