What is the difference between `{}` and `Map.of()`?

364 Views Asked by At

I'm checking Map class. I would like to know the difference between these two below.

Are they exactly the same? What does Map.of do?

Map<int, int> m1 = Map.of({1: 1, 2: 2});
Map<int, int> m2 = <int, int>{1: 1, 2: 2};
1

There are 1 best solutions below

0
On

check the this Dart docs

Map.of() is the same as Map init

  • Map.of()
factory Map.of(Map<K, V> other) = LinkedHashMap<K, V>.of;

But my guess is that since Map initialization is a variable assignment and Map.of() is a function call, the movement of the assembly is that Map assignment will be faster.