Skip to content

Commit

Permalink
Return null when calling toString on a null object (#1693)
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi authored Nov 4, 2024
1 parent 52639fb commit b1f41b4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
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) {
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',
);
});
}

0 comments on commit b1f41b4

Please sign in to comment.