Search code examples
dartmethod-cascades

Any way to get the return value of last method in cascade?


I'm doing something like this:

new A()
  ..methodA()
  ..methodB()
  .toString();

Should this return the result of toString()? Currently it's returning the new A object.


Solution

  • In your code toString() is applied on the result of methodB(). It's like you are doing :

    var func = (o) {
      o.methodA();
      o.methodB().toString();
      return o;
    };
    func(new A());
    

    To do what you want, you have to do something like :

    (new A()
      ..methodA()
      ..methodB()).toString();