1

I am using opencv and flutter for building an app. ffi package is used to implement a bridge and I got error from a tutorial code. I could not find any solution. Could you please kindly help me? I got error when Utf8.fromutf8 and Utf8.toUtf8

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

// C function signatures
typedef _version_func = ffi.Pointer<Utf8> Function();
typedef _process_image_func = ffi.Void Function(ffi.Pointer<Utf8>, ffi.Pointer<Utf8>);

// Dart function signatures
typedef _VersionFunc = ffi.Pointer<Utf8> Function();
typedef _ProcessImageFunc = void Function(ffi.Pointer<Utf8>, ffi.Pointer<Utf8>);

// Getting a library that holds needed symbols
ffi.DynamicLibrary _lib = Platform.isAndroid
    ? ffi.DynamicLibrary.open('libnative_opencv.so')
    : ffi.DynamicLibrary.process();

// Looking for the functions
final _VersionFunc _version = _lib
    .lookup<ffi.NativeFunction<_version_func>>('version')
    .asFunction();
final _ProcessImageFunc _processImage = _lib
    .lookup<ffi.NativeFunction<_process_image_func>>('process_image')
    .asFunction();

String opencvVersion() {
  return Utf8.fromUtf8(_version());
}

void processImage(ProcessImageArguments args) {
  _processImage(Utf8.toUtf8(args.inputPath), Utf8.toUtf8(args.outputPath));
}

error

Error: Method not found: 'Utf8.fromUtf8'.
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Muhtar
  • 1,506
  • 1
  • 8
  • 33

1 Answers1

3

The FFi package from this tutorial is probably outdated. Dart FFI 1.1.1 has the methods toString(), toDartString() and toNativeUtf8(), in the Pointer<Utf8> type.

Checkout here.

It should look like this:

String opencvVersion() {
  return _version().toDartString();
}

void processImage(ProcessImageArguments args) {
  _processImage(args.inputPath.toNativeUtf8(), args.outputPath.toNativeUtf8());
}