dart advantage of a factory constructor identifier

前端 未结 3 2150
小蘑菇
小蘑菇 2020-11-29 04:48

I\'ve been investigating JSON parsing for my Flutter app and have a question about factory constructors that I can\'t resolve. I\'m trying to understand the advantage of usi

相关标签:
3条回答
  • 2020-11-29 04:54

    After I've been noticing and wondering the same, and given I don't think the other answers actually answer the question ("I've been investigating JSON parsing [...] I'm trying to understand the advantage of using a factory constructor verses a plain constructor"), here my try:

    there's no advantage or difference that I could see or understand, when parsing json, in using a factory constructor instead of a plain constructor. I tried both, and both works fine, with all the types of parameters. I decided eventually to adopt the factory constructor, because of the convenience of how the code is written, and readability, but it's a matter of choice and both will work fine in all the cases.

    0 讨论(0)
  • 2020-11-29 04:57

    A factory constructor vs. a normal constructor

    • A factory constructor invokes another constructor.
    • Since a factory constructor does not directly create a new instance, it cannot use a constructor initializer list.
    • A normal constructor always returns a new instance of the class. A factory constructor is permitted to return an existing instance, an instance of a derived class, or null. (However, some people dislike returning null from a factory constructor.)

    A factory constructor vs. a static method

    • A factory constructor can be the unnamed, default constructor of a class.
    • A factory constructor can be used with new. (But using new is now discouraged.)
    • Static methods can be used to create tear-offs (i.e., they can be used as callbacks) but constructors currently can't.
    • Static methods can be async. (A factory constructor must return a type of its class, so it cannot return a Future.)
    • Factory constructors can be declared const.
    • In generated dartdoc documentation, a factory constructor obviously will be listed in the "Constructors" section (which is prominently at the top) whereas a static method will be in the "Static Methods" section (which currently is buried at the bottom).
    0 讨论(0)
  • 2020-11-29 05:14

    A normal constructor always returns a new instance of the current class (except when the constructor throws an exception).

    A factory constructor is quite similar to a static method with the differences that it

    • can only return an instance of the current class or one of its subclasses
    • can be invoked with new but that is now less relevant since new became optional.
    • has no initializer list (no : super())

    So a factory constructor can be used

    • to create instances of subclasses (for example depending on the passed parameter
    • to return a cached instance instead of a new one
    • to prepare calculated values to forward them as parameters to a normal constructor so that final fields can be initialized with them. This is often used to work around limitations of what can be done in an initializer list of a normal constructor (like error handling).

    In your example this code

      studentId: parsedJson['id'],
      studentName : parsedJson['name'],
      studentScores : parsedJson ['score']
    

    could be moved to the body of a normal constructor because no final fields need to be initialized.

    0 讨论(0)
提交回复
热议问题