问题
What is difference between writing code using dart:ffi in flutter or writing code with the method chanel in flutter.
回答1:
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.
来源:https://stackoverflow.com/questions/61899563/difference-between-writing-platform-specific-code-vs-dartffi-code