Search code examples
dart

List use of double dot (.) in dart?


Sometimes I see this List list = [];

Then list..add(color)

What's the difference between using 1 dot(.) and 2 dot(..)?


Solution

  • .. is known as cascade notation. It allows you to not repeat the same target if you want to call several methods on the same object.

    List list = [];
    list.add(color1);
    list.add(color2);
    list.add(color3);
    list.add(color4);
    
    // with cascade
    
    List list = [];
    list
      ..add(color1)
      ..add(color2)
      ..add(color3)
      ..add(color4);