Method Cascades in Dart

Dart, Google Flutter, Method Cascades -

Method Cascades in Dart

Introduction

Method cascades can help with the brevity of your code.

Lets Run Some Code

class Logger {
  void log(dynamic v){
    print(DateTime.now().toString() + ' ' + v);
  }
}
main(){
  
  // Without method cascades
  new Logger().log('program started');
  new Logger().log('doing something');
  new Logger().log('program finished');
  
  // With method cascades
  new Logger()
    ..log('program started')
    ..log('going something')
    ..log('program finished');
}

What Happens When You Run the Code

2018-12-30 09:28:39.686 program started 
2018-12-30 09:28:39.686 doing something 
2018-12-30 09:28:39.686 program finished 
2018-12-30 09:28:39.686 program started 
2018-12-30 09:28:39.686 going something 
2018-12-30 09:28:39.686 program finished