Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between writing platform specific code vs dart:ffi code

What is difference between writing code using dart:ffi in flutter or writing code with the method chanel in flutter.

like image 822
Pratik Mohite Avatar asked Nov 06 '22 07:11

Pratik Mohite


1 Answers

dart:ffi is used to call C/C++ code - typically to reuse an existing C/C++ library - often a CPU intensive operation, like codecs, image processing, cryptography. The C code has limited access to the system APIs.

Method channels are used to access the underlying environment and its APIs - so are written in Kotlin or Java on Android, Swift or Objective C on iOS and macOS and Dart in Flutter Web.

So, for example, if you want to read the battery level of the device, you need to use a method channel and implement the native API call in its native language, e.g. Java for Android. Conversely, if you want to call a C library that implements a cryptographic hash, the easiest way is to use ffi.

Check out the Flutter Week videos for a novel use of ffi to call the Android NDK / Oboe audio library.

like image 77
Richard Heap Avatar answered Nov 15 '22 06:11

Richard Heap