简体   繁体   中英

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());
          }

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());
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM