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

311

Solution 1

You can define private enum property for you to set private info like this, and print it with a function later on. Also don't forget to mark your constructors with 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();
  }
}

If you need this information not as a string, but as enum, you can always remove underscore on it, and make this info public for you to use outside of the class.

Solution 2

You can make your class a Union type using the freezed package and use the folding methods as shown below to see what constructor was used:

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

and the output will be:

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
Share:
311
TryHard
Author by

TryHard

Try Hard :=)

Updated on December 02, 2022

Comments

  • TryHard
    TryHard over 1 year

    I was wondering if I can check which constructor I used to create the created element in an if statement in dart.

    A simple example of what I want to do:

    class Employee {
      int id;
      String name;
      String title;
    
      Employee.id(this.id);
    
      Employee.name(this.name);
    
      Employee.title(this.title);
    }
    

    Now I have an if statement somewhere in my code and want to check if I used the constructor Employee.id. In this case I would do something, somehow like this:

    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")
    }
    

    Is there a way to do this? Thank you for your answer.

  • TryHard
    TryHard about 4 years
    Thank you very much
  • TryHard
    TryHard about 4 years
    Thank you very much!