'Flutter display data from List once if data is repeated
I am fetching data from a database, and let's say the fetched data then return a list which contains List fetchedData = [2, 4, 6, 1, 2, 5, 2], I then want to display the data with a ListView.builder as so
return ListView.builder(
itemCount: fetchedData.length,
itemBuilder: (context, index) {
return Text(
fetchedData[index],
);
},
),
But the list contains the number 2 more than once, so the number 2 would in this case be displayed three times, but I would only want to display it once, is this possible?
Solution 1:[1]
Try to create a Set from your list, like
var fetchedSet = Set.from(fetchedData);
then iterate like
return Text(
fetchedSet.elementAt(index)
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Csisanyi |
