Assume following class:
@freezed
abstract class TestClass implements _$TestClass {
const TestClass._();
const factory TestClass({
@Default('MyName') String testString,
// MyChildTest child,
}) = _TestClass;
static TestClass getInstance({String x}) {
return TestClass(testString: x);
}
}
The class throws an assertion error when I return its instance from the static method.
The following _WORKS_:
TestClass m = TestClass.getInstance(x: 'Something');
While, the following throws assertion error: _'testString != null': is not true._
TestClass m = TestClass.getInstance();
Expected behavior:
@Default, it should take 'MyName' automatically.My intention to use static method is, to not allow the user instantiate the class with all properties. Some properties would be generated (e.g., Some random string, etc.)
The issue is not Freezed but Dart here.
You should instead write:
static TestClass getInstance({String x = 'MyName' }) {
return TestClass(testString: x);
}
Hi @rrousselGit thanks. I also tried to have a look and found this:
https://github.com/dart-lang/sdk/issues/33918
And, I came to know that this is not Freezed issue.
In above link, there are some suggestions.
Currently, we are doing like this in generated class:
const _$_TestClass({this.testString = 'default_value'})
: assert(myname != null),
super._();
My proposal is, can we do something like this?
const _$_TestClass({String tempTestString = 'default_value'})
: this.testString = tempTestString ?? 'default_value'
super._();
As you can see, the assert is removed.
If @nullable is provided, the default behavior (the current one) seems OK.
Your thoughts?
I don't think that's a good idea, because that would prevent people from assigning null to the property
But, we can use @nullable for that.
If any field contains @nullable, default behavior to be used.
If not @nullable, then only use newly proposed syntax.
But that won't work once we have non-nullable types
On Sun, May 10, 2020, 06:18 Ravi Kavaiya notifications@github.com wrote:
But, we can use @nullable https://github.com/nullable for that.
If any field contains, @nullable https://github.com/nullable, default
behavior to be used.
If not @nullable https://github.com/nullable, then only use newly
proposed syntax.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/rrousselGit/freezed/issues/166#issuecomment-626275157,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AEZ3I3OE43HVC7UGVJR6MF3RQY2KPANCNFSM4M5CS52A
.
@rrousselGit I like @RaviKavaiya 's idea. This would be very much helpful to developers by eliminating most boilerplate code.
By the way, I don't understand :
But that won't work once we have non-nullable types
Can you please elaborate, how @RaviKavaiya 's approach would not work in NNBD?
static TestClass getInstance({String x }) {
return TestClass(testString: x);
}
This code won't compile anymore.
To fix the error, you will have to revert back to writing:
static TestClass getInstance({String x = 'MyName' }) {
return TestClass(testString: x);
}
So that would be only a temporary solution.
I don't plan on doing anything related to this, so I'll close it for now