0

I'm using an API which returns the value like this.

[
    {
        "store": "AMAZON"
    },
    {
        "store": "FLIPKART"
    },
    {
        "store": "WALMART"
    },
    {
        "store": "ALIBABA"
    },

]

I need this to be in a drop down. I need a drop down button with this API data in it. Some one help please. I have tried many ways but nothing worked.

2
  • Which way have you tried? Put code to get help please!! Commented Nov 18, 2020 at 12:42
  • @Hakiz'a I added the link of my previous question stackoverflow.com/questions/64888506/… Please help me. From morning nothing worked out. I'm pissed Commented Nov 18, 2020 at 12:44

2 Answers 2

1

nigale try code:

List<String> markets = []; // Or  var markets = [];

  String _mySelection;
  @override
  void initState() {
    buidDropDownItems();
    super.initState();
  }

// 
void buidDropDownItems() async {
    markets = await retrievedata.getMarket();
    // Refresh the UI if the State object has been created
    if(mounted){
      setState(() {});
    }
}

 child: DropdownButton(
          items: markets.map<DropdownMenuItem<String>>((String val){
                  return DropdownMenuItem<String>(value: val, child: Text(val));
                }).toList(), // Get items from the available data in markets variable
          onChanged: (sto) {
            setState(() {
              _mySelection = sto;
            });
          },
          value: _mySelection,
          hint: Text('Please select the store: '),
        ),

The function retrievedata.getMarket(); is returning Future<dynamic> which dynamic is your markets list.

Sign up to request clarification or add additional context in comments.

6 Comments

It causes another error. ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following NoSuchMethodError was thrown building homePage(dirty, state: homePageState#f375b): The method 'map' was called on null. Receiver: null Tried calling: map<DropdownMenuItem<String>>(Closure: (String) => DropdownMenuItem<String>) The relevant error-causing widget was: homePage
Make sure to have done like this List<String> markets = []; // Or var markets = []; initialize the markets list
Then this error arises. type '(String) => DropdownMenuItem<String>' is not a subtype of type '(dynamic) => DropdownMenuItem<String>' of 'f' Like the same from morning. Please solve this alone.
After some modification. Now new error is produced. There should be exactly one item with [DropdownButton]'s value: . Either zero or 2 or more [DropdownMenuItem]s were detected with the same value 'package:flutter/src/material/dropdown.dart': Failed assertion: line 834 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) { return item.value == value; }).length == 1' The relevant error-causing widget was: homePage
Check my working example here codeshare.io/GbZB3b
|
0

let's say that this array is retrieved in a variable called data

 List<String>list=[];
for(var itemData in data){
  list.add(itemData['store']);
}

And wala, Now list conation array of String ready to be used

2 Comments

please see the comment that I have added a link to my previous question.
when I tried API returns the after the UI has been rendered. Please suggest me some solutions.

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.