Flutter:这个概念的名称是什么?请告诉我要寻找什么以获得更好的理解

Flutter: What is the name of the concept? please tell me what to look for better understanding

class ApiResponse<T> {
  Status status;
  T? data;
  String? message;

  ApiResponse.initial(this.message) : status = Status.INITIAL; //i dont know what is happening here exactly. please explain.

  ApiResponse.loading(this.message) : status = Status.LOADING; //same

  ApiResponse.completed(this.data) : status = Status.COMPLETED; //same

  ApiResponse.error(this.message) : status = Status.ERROR; //same

  @override
  String toString() {
    return "Status : $status \n Message : $message \n Data : $data";
  }
}

enum Status { INITIAL, LOADING, COMPLETED, ERROR }

请查看评论。我评论了我不明白的地方。我只是想猜测到底发生了什么。但我需要更好地了解才能开始实施它。任何人都可以分享我可以更好地了解这件事的视频或文章。

这些就是所谓的 named constructors 它们最常用于在构建 类 时设置特定的默认值。

您可以将示例用作:

final apiResponse = ApiResponse<String>.initial("message");

这将创建 ApiResponse 的新实例,其中 status 等于 Status.INITIAL