Search code examples
dartflame

Two dots in dart (..)


I was reading the example game code in the Flame game engine.

Code:

MyGame() {
  add(Square()
    ..x = 100
    ..y = 100);
}

this was the code in the repository (line 59-63): https://github.com/flame-engine/flame/blob/master/example/lib/main.dart

I have seen this notation earlier but I never understood what (..x and ..y) means.

What does .. represent here?


Solution

  • This is called a cascade notation in Dart, instead of just setting the value like . would, it also returns the object you are working on.

    So since add here wants a Component, Square (which is a component) is first created and then x is set to 100 and returns the same square and then y is set to 100 and returns the same square again which is then sent in to add.

    https://dart.dev/guides/language/language-tour#cascade-notation-