You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given that factory constructors can't be async, I only see three options :
Option 1 : Use the builder pattern
We generate a "Builder" that you should instantiate and then call an async method (for example runValidations) to get your modddel instance.
final builder =AgeBuilder(20);
final age =await builder.runValidations();
Advantages :
Ability to lazily validate a modddel (Although this can be easily accomplished by the user using a package like lazy_evaluation or any other way).
Disadvantages :
Boilerplate code +++
Different syntax for instantiating a modddel with sync validations versus async validations
Option 2 : Initialize the modddel manually
We add an init method to the modddel that the user should call, and its returned value is the initialized modddel.
final age =awaitAge(20).init();
Advantages :
Less boilerplate code
Disadvantages :
⚠️ RUNTIME ERRORS : If the user forgets to call init or accidentally uses the instance created with the factory constructor (Age(20)) , there will be runtime errors that may be hard to debug
Again : different syntax for instantiating a sync modddel versus async
Option 3 : Ditch the factory constructors for async methods
We replace the factory constructors with static methods :
// Instead of a factory constructor : factoryAge(int value) {
return_$Age._create(
value: value,
);
}
// We use a static methodstaticAgecreate(int value) {
// To accompany this change, this static method is no longer private// (it never needed to be private since the mixin `_$Age` is private)return_$Age.create(
value: value,
);
}
The name of the method should be create for solo modddels, and create{UnionCaseName} for unions (ex for a Weather union : createSunny, createRainy...).
Then, these static methods can easily be made async :
// Change the return type to a future, and optionally add the async keyword if needed staticFuture<Age> create(int value) {
return_$Age.create(
value: value,
);
}
And then for making an instance of the modddel :
// If sync : final age =Age.create(20);
// If async :final age =awaitAge.create(20);
Advantages :
Same syntax for instantiating sync and async modddels (you only add await)
Right now I really don't know which way we should go.
Option 1 : For the builder, we can either generate it, or make it the annotated class itself. If we generate it, the factory constructors should be made private so that they're never called directly (it's kinda weird). If we make it the annotated class itself, then devs won't be able to add members (methods, getters...) to the modddel (they can only add instance members using extensions).
Right now, all validation methods are synchronous, and there is no way to make them async.
Given that factory constructors can't be async, I only see three options :
Option 1 : Use the builder pattern
We generate a "Builder" that you should instantiate and then call an async method (for example
runValidations
) to get your modddel instance.Option 2 : Initialize the modddel manually
We add an
init
method to the modddel that the user should call, and its returned value is the initialized modddel.init
or accidentally uses the instance created with the factory constructor (Age(20)
) , there will be runtime errors that may be hard to debugOption 3 : Ditch the factory constructors for async methods
We replace the factory constructors with static methods :
The name of the method should be
create
for solo modddels, andcreate{UnionCaseName}
for unions (ex for aWeather
union :createSunny
,createRainy
...).Then, these static methods can easily be made async :
And then for making an instance of the modddel :
_$Age.create
) has the same name as the static methodAge.create(20)
vsAge(20)
).The text was updated successfully, but these errors were encountered: