Fill items from a Dropdownbutton with a Json value

Asked

Viewed 110 times

2

I have a field DropDownMenu and would like to fill in the Items of it with the values of my Json. I have this code:

String _mySelection;
List<Map> _myJson = [{"id":0,"name":"<New>"},{"id":1,"name":"Test Practice"}];`

That works in the DropDownMenu:

items: _myJson.map((Map map) {
   return new DropdownMenuItem<String>(
      value: map["id"].toString(),
       child: new Text(
       map["name"],
      ),
    );
}).toList(),`

But what I wanted was instead of passing the Json mounted on _mySelection would pass the value returned from my class ResultLogin

static ResultLogin fromJson(Map<String, dynamic>  json) {

   List<ZLoginResultSchema> schemasList = [];

   for (var s in json['schemas']) {
     schemasList.add(ZLoginResultSchema.fromJson(s));
   }
}

My class ZLoginResultSchema where I assemble the list of schemas:

class ZLoginResultSchema  {
  final String name;
  final String fullname;

  ZLoginResultSchema({this.name, this.fullname});

  ZLoginResultSchema.fromJson(Map<String, dynamic> json):
    name    = json['name'],
    fullname = json['fullname'];

  Map<String, dynamic> toJson() =>
  {
    'name': name,
    'fullname': fullname,
  };
}

How can I take the value of schemaList class ResultLogin, and pass the Function _myJson of my Page?

1 answer

3


I don’t know if I fully understand what you’re up to, especially since I don’t understand what your _mySelection, if it is a function or what. But from what I understand you want to return the values of schemaList which is nothing more than a list of Map’s:

static List resultLogin fromJson(Map<String, dynamic>  json) {

   List<ZLoginResultSchema> schemasList = [];

   for (var s in json['schemas']) {
     schemasList.add(ZLoginResultSchema.fromJson(s));
   }
return schemasList;
}

A Map list of type will be returned ZLoginResultSchema. And you can get it normally from the list

  • _Myselection is only the variable that I will store the selected value, I was able to get the list values by directly passing the list class in the map. items: schemas.map((ZLoginResultSchema map) {&#xA; return new DropdownMenuItem<String>(&#xA; value: map.name,&#xA; child: new Text(map.fullname),&#xA; );&#xA; }).toList()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.