Skip to content

Return null when calling toString on a null object #1693

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

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/jni/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
through method channels. You can send the address of the pointer as `long` and
reconstruct the class using the helper method.
- Fixed a bug where it would be possible for a type class inference to fail.
- Return 'null' when calling `toString` on a null object.

## 0.12.0

Expand Down
3 changes: 3 additions & 0 deletions pkgs/jni/lib/src/jobject.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ class JObject {
_class.instanceMethodId(r'toString', r'()Ljava/lang/String;');
@override
String toString() {
if (reference.isNull) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you don't like ? : 😉

Copy link
Member Author

@HosseinYousefi HosseinYousefi Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm indifferent :P

return 'null';
}
return _toStringId(this, const JStringType(), [])
.toDartString(releaseOriginal: true);
}
Expand Down
12 changes: 12 additions & 0 deletions pkgs/jni/test/jobject_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,16 @@ void run({required TestRunnerCallback testRunner}) {
throwsA(isA<AssertionError>()),
);
});

testRunner('toString', () {
final long = JLong(1);
expect(
long.toString(),
'1',
);
expect(
JLong.fromReference(jNullReference).toString(),
'null',
);
});
}
Loading