diff --git a/pkgs/jni/CHANGELOG.md b/pkgs/jni/CHANGELOG.md index ab37ff4ef..bee6695c0 100644 --- a/pkgs/jni/CHANGELOG.md +++ b/pkgs/jni/CHANGELOG.md @@ -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 diff --git a/pkgs/jni/lib/src/jobject.dart b/pkgs/jni/lib/src/jobject.dart index 9390b7aee..f25fdb59a 100644 --- a/pkgs/jni/lib/src/jobject.dart +++ b/pkgs/jni/lib/src/jobject.dart @@ -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); } diff --git a/pkgs/jni/test/jobject_test.dart b/pkgs/jni/test/jobject_test.dart index a3ffd2d85..ecb64bdfc 100644 --- a/pkgs/jni/test/jobject_test.dart +++ b/pkgs/jni/test/jobject_test.dart @@ -296,4 +296,16 @@ void run({required TestRunnerCallback testRunner}) { throwsA(isA()), ); }); + + testRunner('toString', () { + final long = JLong(1); + expect( + long.toString(), + '1', + ); + expect( + JLong.fromReference(jNullReference).toString(), + 'null', + ); + }); }