Home > Net >  How to add textfield values in list in object form in flutter
How to add textfield values in list in object form in flutter

Time:12-15

I have two textfields and i want to add them in list in object form like this

{"color":red,"size":L},{"color":blue,"size":S}

here is my code

i created this list

List<dynamic> _colorList = [];

and using it in a button like this

 _colorList.add({"color": currentColor, "size": size.text});

and i need to remove the items from list too so unable to use Map<String, Object>

Error:

The argument type 'Map<String, Object>' can't be assigned to the parameter type 'String'.

please help how to do this.

CodePudding user response:

you have to use Map<String, dynamic> and write a custom delete function.

void main() {
  
  List<Map<String, dynamic>> _colorList = [];
  
  void  deleteByColorName (String colorName)  {
    _colorList.removeWhere((element) {
    return  element["color"] == colorName;
    });
  }
  
  _colorList.add({"color": "colorA", "size": 1});
  _colorList.add({"color": "colorB", "size": 1});
  
  deleteByColorName("colorA");
  
  print(_colorList);
}

  • Related