dontknowhy
dontknowhy

Reputation: 2866

Flutter Bloc, what is the "..add"

create: (_) {
            return NewCarBloc(newCarRepository: NewCarRepository())
                ..add(NewCarFormLoaded());
          }

Why it has 2 dots here?

Why not like below? I tried in various ways, but nothing else works.

create: (_) {
            return NewCarBloc(newCarRepository: NewCarRepository())
                .add(NewCarFormLoaded());
          }

Upvotes: 3

Views: 1608

Answers (1)

Milvintsiss
Milvintsiss

Reputation: 1448

The double dot operator let you call multiple functions on the same object in one instruction. It's named cascade operator.

For more about cascade operator: https://fluttermaster.com/method-chaining-using-cascade-in-dart/

Here your first function is to create the object and the second is "add" function.

If you don't want to use cascade operator you can do this like so:

create: (_) {
        NewCarBloc newCarBloc = NewCarBloc(newCarRepository: NewCarRepository());
        return newCarBlock.add(NewCarFormLoaded());
      }

Upvotes: 2

Related Questions