Skip to content

Commit b1f41b4

Browse files
Return null when calling toString on a null object (#1693)
1 parent 52639fb commit b1f41b4

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

pkgs/jni/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
through method channels. You can send the address of the pointer as `long` and
55
reconstruct the class using the helper method.
66
- Fixed a bug where it would be possible for a type class inference to fail.
7+
- Return 'null' when calling `toString` on a null object.
78

89
## 0.12.0
910

pkgs/jni/lib/src/jobject.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ class JObject {
127127
_class.instanceMethodId(r'toString', r'()Ljava/lang/String;');
128128
@override
129129
String toString() {
130+
if (reference.isNull) {
131+
return 'null';
132+
}
130133
return _toStringId(this, const JStringType(), [])
131134
.toDartString(releaseOriginal: true);
132135
}

pkgs/jni/test/jobject_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,16 @@ void run({required TestRunnerCallback testRunner}) {
296296
throwsA(isA<AssertionError>()),
297297
);
298298
});
299+
300+
testRunner('toString', () {
301+
final long = JLong(1);
302+
expect(
303+
long.toString(),
304+
'1',
305+
);
306+
expect(
307+
JLong.fromReference(jNullReference).toString(),
308+
'null',
309+
);
310+
});
299311
}

0 commit comments

Comments
 (0)