New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamic field throws 'Null check operator used on a null value' #74
Comments
Hi @techouse, |
It does not crash, it just throws all these error messages. |
@techouse Looks like I'm having difficulty replicating the issue and I am wondering if you can guide me on how to see the relevant error messages. |
@numen31337 of course :) The warning only appears when you actually run the code. In the tests it's muted. I have created a repo demonstrating it here https://github.com/techouse/copy_with_extension_nullability_issue_demo Way to reproduce it:
dart run bin/main.dart This will print
|
For the record, the problem mentioned here is not reflected in #74 (comment). The #74 (comment) is representative of the issue #75 which is a different one. This ticket pertains specifically to instances where an object has a dynamic field that was initialised as Test case: import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:test/test.dart' show test, expect;
part 'gen_nullability_test.g.dart';
@CopyWith()
class TestNullability {
TestNullability(
int this.nullableWithNonNullableConstructor,
this.dynamicField,
this.integers,
);
final int? nullableWithNonNullableConstructor;
final dynamic dynamicField;
final List<int> integers;
}
void main() {
test('TestNullability', () {
expect(TestNullability(1, null, [1]).copyWith.integers([2]).dynamicField, null);
});
} Leads to the crash here: dynamicField == const $CopyWithPlaceholder() || dynamicField == null
// ignore: unnecessary_non_null_assertion
? _value.dynamicField! // <--- CRASH
// ignore: cast_nullable_to_non_nullable
: dynamicField as dynamic, |
@numen31337 correct. My memory on this issue is not the freshest anymore. I have added more tests into another branch in the same repo https://github.com/techouse/copy_with_extension_nullability_issue_demo/tree/issue-74 Running
|
Describe the issue
I have a
dynamic
field and the generated code throws aNull check operator used on a null value
exception.Possibly a result of #69 (comment)
Environment details
Dart 2.18.6
To Reproduce
Steps to reproduce the behaviour:
Declare a
dynamic
field in a class, i.e.and the generated code is
This will throw an exception when running unit tests
The exception above refers to the line
where a
null
check operator (!
) is used ondynamic
Expected behaviour
No exception and nullability properly handled on a
dynamic
field.Additional context
The only workaround I have found is to use
Object?
instead ofdynamic
, like so:and the generated code becomes
You can see the workaround in action here
The text was updated successfully, but these errors were encountered: