0

I have a list (userData) which has json parsed values; I want to iterate through the list, and strip down a single key/value pair to reduce the list to a set of common values.

The userData list has json parsed data including countries and continents. I want to produce a reduced list of unique continents (and create a dynamic listview with this reduced list). See my side/drawer code below:

drawer: Drawer(
        // Add a ListView to the drawer with unique continents
        child: ListView.builder(
          itemCount: userData == null ? 0 : userData.length,
          itemBuilder: (BuildContext context, int index) {
            for (var item in userData) {
              continents.add(userData[item]['continent']);
            }
            continents.toSet().toList();

            return Container(
                width: 130,
                alignment: Alignment.center,
                child: Text(
                  "${continents[index]}",  //unique continent should be here
                  style: TextStyle(
                    fontSize: 20.0,
                    fontWeight: FontWeight.w500,
                  ),
                ));
          },
        ),
      ),

Thanks in advance for any assistance!

3
  • 1
    Can you add your userData? and what problem are you facing? Commented May 5, 2020 at 11:30
  • Thanks Josteve. Think of it like ``` List fruits = ['apple', 'orange', 'apple', 'pear', 'apple'];``` and I want to reduce this to a uniqueFruits = ['apple', 'orange', 'pear'] list (within a ListView widget). Commented May 5, 2020 at 22:59
  • Miler what error are you getting? Commented May 5, 2020 at 23:01

1 Answer 1

1

Assign continent back to continents.toSet().toList();

Like

continents = continents.toSet().toList();
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.