2
final _set = [1, 1, 2, 3, 4].toSet(); 

besides effective dart suggesting prefer_collection_literals

it really looks like java or c# rather than dart (no offense)

does anybody knows

How to convert a List to Set using literals

(suggesting to add // ignore: prefer_collection_literals isn't an answer)

2

2 Answers 2

11

You can do something like this:

main() { final list = [1, 1, 2, 3, 4]; final _set = {...list}; print('set: $_set'); // set: {1, 2, 3, 4} } 
Sign up to request clarification or add additional context in comments.

Comments

1

You can write the same code as final _set = {1, 1, 2, 3, 4};.

When you write {key: value, key: value} that is a Map literal, but if you just do {value, value, ...} that becomes a Set literal.

3 Comments

thanks, but you are creating a set, not converting a list to set
@FrancescoIapicca It would help if you explained why you need to bother using a List literal at all instead of directly using a Set literal. Your question doesn't make much sense; either you're initializing a Set from a literal, in which case you should just use a Set literal directly, or you're initializing a Set from some runtime List, in which case Set.of() or List.toSet() would work just as well.
in my specific case, in an Assert(...) I need to check that a list has no duplicate, I struggle to see how the use case this is relevant to the answer that I already accepted stackoverflow.com/a/66226798/9836706

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.