vb10
vb10

Reputation: 681

How to write chain functions DART?

I'm writing the flutter application and need a chain function. I found some solutions but it didn't work for me. How it's written?

For example.(i written custom get function)

 get().addHeader(value:xx).addHeader(value:xxx)

I'm using HTTP helper or String helper functions.

Upvotes: 2

Views: 2549

Answers (1)

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76183

As adviced in Effective Dart:

AVOID returning this from methods just to enable a fluent interface.

Method cascades are a better solution for chaining method calls.

In your case

class HttpCall {
  void addHeader(String name, String value) { ... }
}

main() {
  // get() returns a HttpCall
  get()
    ..addHeader('name1', 'value1')
    ..addHeader('name2', 'value2');
}

Upvotes: 5

Related Questions