简体   繁体   English

如何编写DART链函数?

[英]How to write chain functions DART?

I'm writing the flutter application and need a chain function. 我正在编写flutter应用程序并需要一个链函数。 I found some solutions but it didn't work for me. 我找到了一些解决方案,但它对我不起作用。 How it's written? 怎么写的?

For example.(i written custom get function) 例如。(我写的自定义get函数)

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

I'm using HTTP helper or String helper functions. 我正在使用HTTP帮助程序或字符串帮助程序函数。

As adviced in Effective Dart : 正如Effective Dart中的建议

AVOID returning this from methods just to enable a fluent interface. AVOID从方法中返回this只是为了启用流畅的界面。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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