1

While looking at Reso Coder's TDD Clean Architecture tutorial, I came across some factory code that seems to do the same thing as a normal method. What's the benefit of using the factory keyword, and is there a difference between:

  factory NumberTriviaModel.fromJson(Map<String, dynamic> json) {
    return NumberTriviaModel(
      text: json['text'],
      number: (json['number'] as num).toInt(),
    );
  }

and

NumberTriviaModel NumberTriviaFromJson(Map<String, dynamic> json) {
    return NumberTriviaModel(
      text: json['text'],
      number: (json['number'] as num).toInt(),
    );
  }

?

Philip
  • 638
  • 1
  • 8
  • 22
  • 1
    https://stackoverflow.com/questions/52299304/dart-advantage-of-a-factory-constructor-identifier this? – Adelina Nov 30 '19 at 13:30
  • I see, thanks. So in this case the factory keyword is not necessary. But in more complex cases it could be used to return an instance of a subclass or return an existing object from a cache – Philip Nov 30 '19 at 13:54
  • 1
    Does this answer your question? [dart advantage of a factory constructor identifier](https://stackoverflow.com/questions/52299304/dart-advantage-of-a-factory-constructor-identifier) – frohlichcortezh May 31 '21 at 18:47

0 Answers0