Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR DART

factory in dart

// A factory constructor will help you to create a custom instance of a class

void main() {
  
  Point point = Point.fromMap({"x": 30, "y": 90});
  Point point2 = Point(10, 20);

  print({"Taken from Point fromMap factory",point});
  print({"Taken from Point constructor", point2});
  
}

class Point {
  double x, y;
  Point(this.x, this.y);

  factory Point.fromMap(Map<String, double> json) {
    return Point(json['x']!, json['y']!);
  }

  @override
  String toString() {
    return "toString: $x : $y";
  }
}
 
PREVIOUS NEXT
Tagged: #factory #dart
ADD COMMENT
Topic
Name
9+6 =