【问题标题】:Flutter, what is the ":"? [duplicate]颤振,“:”是什么? [复制]
【发布时间】:2021-04-15 12:35:18
【问题描述】:
enum ListStatus { loading, success, failure }

class ListState extends Equatable {
  const ListState._({
    this.status = ListStatus.loading,
    this.items = const <Item>[],
  });

  const ListState.loading() : this._();

  const ListState.success(List<Item> items)
      : this._(status: ListStatus.success, items: items);

  const ListState.failure() : this._(status: ListStatus.failure);

  final ListStatus status;
  final List<Item> items;

  @override
  List<Object> get props => [status, items];
}

1 中的“._”是什么

ListState._({...})   ?

2 这些函数旁边的“:”是什么?什么时候使用?

【问题讨论】:

标签: dart


【解决方案1】:
ListState._({...})

这是一个私有命名构造函数,这意味着您不能从当前库之外实例化此类对象。


const ListState.loading() : this._();

这也是一个命名构造函数(虽然不是私有的),这里 this._()) 只是将调用委托给私有命名构造函数。

【讨论】:

猜你喜欢
  • 2021-02-16
  • 2020-09-22
  • 2020-11-19
  • 2019-07-10
  • 2018-11-08
  • 2022-08-06
  • 2021-06-21
  • 1970-01-01
  • 2021-11-19
相关资源
最近更新 更多