3

How do I populate this JSON list into dropdown button?

{
    "status": true,
    "message": "success",
    "data": {
        "list": [
            {
                "idattribute": "2",
                "attrName": "BBQ"
            },
            {
                "idattribute": "1",
                "attrName": "FRUIT JUICE"
            }
        ]
    }
}

1 Answer 1

4

enter image description here

class _YourPageState extends State<YourPage> {
  Map yourJson = {
    "status": true,
    "message": "success",
    "data": {
      "list": [
        {"idattribute": "2", "attrName": "BBQ"},
        {"idattribute": "1", "attrName": "FRUIT JUICE"}
      ]
    }
  };
  int _value = 1;
  List<DropdownMenuItem<int>> _menuItems;

  @override
  void initState() {
    super.initState();

    List dataList = yourJson["data"]["list"];
    _menuItems = List.generate(
      dataList.length,
          (i) => DropdownMenuItem(
        value: int.parse(dataList[i]["idattribute"]),
        child: Text("${dataList[i]["attrName"]}"),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: DropdownButton<int>(
          items: _menuItems,
          value: _value,
          onChanged: (value) => setState(() => _value = value),
        ),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.