DART ffi 处理C pointer str为dart string

要引入两个模块,两个是不同的:

import 'dart:ffi';

import 'package:ffi/ffi.dart';

pointer char to string:

  var screenStr = (xx.GetSomeStrFun().cast<Utf8>().toDartString());

  var s = screenStr.split("x");

  _screenSize = {"width": double.parse(s[0]),"height":double.parse(s[1])};

  print(_screenSize);
//加载符号
DynamicLibrary nativeApi = Platform.isAndroid
    ? DynamicLibrary.open("libnative_ffi.so")
    : DynamicLibrary.process();
    
//查找目标函数
FFI_greetingString greetingFunc = nativeApi
	.lookupFunction<Native_greetingString, FFI_greetingString>("greetString");
    
//调用 greetString 函数,并将结果转为 Dart String.
Pointer<Int8> result = greetingFunc();
String greeting = result.cast<Utf8>().toDartString();

//打印结果
print("greeting=$greeting");
    
//输出结果
//greeting=Hello from Native

参考:https://blog.csdn.net/eieihihi/article/details/119152003

你可能感兴趣的:(myself,flutter/dart,c语言,android,开发语言)