What is 'Native Type' for 'char*' in dart FFI?


I have a function like this in C:

char* getString() {
    return "SOME_STRING";

now I want to invoke it by FFI in dart, and this is my code:

import 'dart:io';
import 'dart:ffi';

void main(List<String> arguments) {

final DynamicLibrary nativeAppTokenLib = Platform.isAndroid
    ? DynamicLibrary.open('lib_native_get_string.so')
    : DynamicLibrary.process();

final String Function() getString = nativeAppTokenLib
    .lookup<NativeFunction<HERE!!! Function()>>('getString')

I wonder what should I put instead of HERE!!! as native type?



import 'dart:ffi';
import 'dart:io';
import "package:ffi/ffi.dart";


final Pointer<Utf8> Function() _getString = nativeAppTokenLib
    .lookup<NativeFunction<Pointer<Utf8> Function()>>('getString')
String getString() => _getString().toDartString();

This uses package:ffi‘s Utf8 type to represent characters. The toDartString extension method on Pointer<Utf8> is the intended way to convert those to a string.

Answered By – lrn

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *