diff --git a/pkgs/jni/example/lib/main.dart b/pkgs/jni/example/lib/main.dart index b406a9d3e..bd6fb00bc 100644 --- a/pkgs/jni/example/lib/main.dart +++ b/pkgs/jni/example/lib/main.dart @@ -7,9 +7,17 @@ import 'dart:ffi'; import 'dart:io'; +import 'package:ffi/ffi.dart'; import 'package:flutter/material.dart'; import 'package:jni/jni.dart'; +extension on String { + /// Returns a Utf-8 encoded `Pointer` with contents same as this string. + Pointer toNativeChars(Allocator allocator) { + return toNativeUtf8(allocator: allocator).cast(); + } +} + // An example of calling JNI methods using low level primitives. // GlobalJniEnv is a thin abstraction over JNIEnv in JNI C API. // @@ -18,7 +26,7 @@ import 'package:jni/jni.dart'; String toJavaStringUsingEnv(int n) => using((arena) { final env = Jni.env; final cls = env.FindClass("java/lang/String".toNativeChars(arena)); - final mId = env.GetStaticMethodID(cls, "valueOf".toNativeChars(), + final mId = env.GetStaticMethodID(cls, "valueOf".toNativeChars(arena), "(I)Ljava/lang/String;".toNativeChars(arena)); final i = arena(); i.ref.i = n;