簡體   English   中英

如何檢查是否使用 dart 中的命名構造函數創建了元素?

[英]How do I check if an element was created with a named constructor in dart?

我想知道是否可以在 dart 的 if 語句中檢查我使用哪個構造函數來創建創建的元素。

我想做的一個簡單的例子:

class Employee {
  int id;
  String name;
  String title;

  Employee.id(this.id);

  Employee.name(this.name);

  Employee.title(this.title);
}

現在我的代碼中有一個 if 語句,並想檢查我是否使用了構造函數 Employee.id。 在這種情況下,我會做一些事情,就像這樣:

Employee e = new Employee.id(1)

//check if e was created with Employee.id constructur
if (e == Emploee.id) { 
   print(e.id)
} else {
   print("no id")
}

有沒有辦法做到這一點? 謝謝您的回答。

您可以為您定義私有枚舉屬性來設置這樣的私有信息,並稍后使用 function 打印它。 也不要忘記用factory標記你的構造函數。

enum _ConstructorType {
  Identifier,
  Name,
  Title,
}

class Employee {
  int id;
  String name;
  String title;
  _ConstructorType _constructorType;

  factory Employee.id(id) {
    return Employee._privateConstructor(_ConstructorType.Identifier, id: id);
  }

  factory Employee.name(name) {
    return Employee._privateConstructor(_ConstructorType.Name, name: name);
  }

  factory Employee.title(title) {
    return Employee._privateConstructor(_ConstructorType.Title, title: title);
  }

  Employee._privateConstructor(this._constructorType,
      {this.id, this.name, this.title});

  String constructorDescription() {
    return this._constructorType.toString();
  }
}

如果您需要此信息不是作為字符串,而是作為枚舉,您始終可以刪除其上的下划線,並將此信息公開以供您在 class 之外使用。

您可以使用凍結的 package 使您的 class 成為Union type ,並使用如下所示的折疊方法來查看使用了什么構造函數:

 import 'package:freezed_annotation/freezed_annotation.dart';

part 'tst.freezed.dart';

@freezed
abstract class Employee with _$Employee {
  const factory Employee.id(int id) = IdEmployee;

  const factory Employee.name(String name) = NameEmployee;

  const factory Employee.title(String title) = TitleEmployee;
}

void main() {
  Employee employee1 = Employee.id(0);
  Employee employee2 = Employee.name('some name');
  Employee employee3 = Employee.title('some title');

  employee1.when(
    id: (int id) => print('created using id contsrutor and id= $id'),
    name: (String name) => print('created using name const and name = $name'),
    title: (String title)=>print('created using title const and title = $title'),
  );//prints the first statement

  employee2.when(
    id: (int id) => print('created using id contsrutor and id= $id'),
    name: (String name) => print('created using name const and name = $name'),
    title: (String title)=>print('created using title const and title = $title'),
  );//prints the second statement

  employee3.when(
    id: (int id) => print('created using id contsrutor and id= $id'),
    name: (String name) => print('created using name const and name = $name'),
    title: (String title)=>print('created using title const and title = $title'),
  );//prints the third statement


  print(employee1 is IdEmployee);
  print(employee1 is NameEmployee);
}

output 將是:

created using id contsrutor and id= 0
created using name const and name = some name
created using title const and title = some title
true
false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM