Skip to content
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
4 changes: 2 additions & 2 deletions pkgs/jni/lib/src/jobject.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ class JObject {
void releasedBy(Arena arena) => arena.onReleaseAll(release);
}

extension JObjectUseExtension<T extends JObject> on T {
extension JObjectUseExtension<T extends JObject?> on T {
/// Applies [callback] on this object and then delete the underlying JNI
/// reference, returning the result of [callback].
R use<R>(R Function(T) callback) {
try {
final result = callback(this);
return result;
} finally {
release();
this?.release();
}
}
}
2 changes: 2 additions & 0 deletions pkgs/jni/test/jobject_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ void run({required TestRunnerCallback testRunner}) {
});
});
expect(randomInt, lessThan(15));
const JObject? nullableJObject = null;
expect(nullableJObject.use((_) => 'foo'), equals('foo'));
});

// The JObject and JClass have NativeFinalizer. However, it's possible to
Expand Down
Loading