飞镖厂构造带/不带关键字`new`,有什么区别?(Dart factory constructors

2019-09-30 21:15发布

我碰到下面的代码示例从flutter_redux示例代码。 有一个很难理解为什么factory SearchState.initial()与返回new关键字,而factory SearchState.loading()factory SearchState.error()没有。

class SearchState {
  final SearchResult result;
  final bool hasError;
  final bool isLoading;

  SearchState({
    this.result,
    this.hasError = false,
    this.isLoading = false,
  });

  factory SearchState.initial() =>
      new SearchState(result: SearchResult.noTerm());

  factory SearchState.loading() => SearchState(isLoading: true);

  factory SearchState.error() => SearchState(hasError: true);
}

刚刚发现达特语言导游不太这种情况有帮助,以及飞镖语言规范是太晦涩。

Answer 1:

从行情有效镖指南:

飞镖2使新的关键字可选。 即使在达特1,它的意思是从来没有清楚,因为工厂构造意味着一个新的调用可能还没有真正回到一个新的对象。

语言为了使迁移痛苦少,但考虑弃用,从代码中删除它仍然允许新的。



Answer 2:

没有区别。 在飞镖2, new关键字是成为可选。 如果调用构造函数时,你不写东西,它的隐含假设是new

大多数代码仍然使用new ,因为它是在达特1必需的,有些人甚至喜欢它,所以你会看到代码使用和不使用new



文章来源: Dart factory constructors with / without keyword `new`, what's the difference?
标签: dart