0

my problem is following:

class Data {
  final String? image, title, link;
  final Color? color;

  Data({
    this.title,
    this.image,
    this.link,
    this.color,
  });
}


List dataList = [
  Data(
      title: "XXX",
      image: "XXX",
      link: "XXX",
      color: XXX),

...

I have this class and list to store my data and however I'm used that way the first time to learn something new. Now I have the problem that I'm totally failing to create a DropDownButton with the data from my list because of the Data() I've in there.

I tried something like this:

@override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: DropdownButton<String>(
          items: dataList.map((Map map) {
              return new DropdownMenuItem<String>(
                value: map["title"].toString(),
                child: new Text(
                  map["name"],
                ),
              );
            }).toList(),
        ),
      ),
    );
  }
}

But I was sure that this option wont work for my example. I think there is now way out to ask you for help. So my exact question is, how can I create a DropDownMenu/Button, where the title of all my Data classes is displayed in a list. I appreciate your help, thanks.

2
  • 1
    you want to get data from API and display it into dropdown? Commented Sep 7, 2021 at 9:01
  • I want to display each title of my Data item from my dataList in that dropdownmenu. See that in the first code section. So there should be displayed that "XXX" from each title. Commented Sep 7, 2021 at 9:05

1 Answer 1

2

Your list is of generic type Data, but you are treating it as a Map.

@override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: DropdownButton<String>(
          items: dataList.map((data) {
              return DropdownMenuItem<String>(
                value: data.title.toString(),
                child: Text(
                  data.name,
                ),
              );
            }).toList(),
        ),
      ),
    );
  }
}

It should work now.

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.