text
stringlengths
1
372
this is done using a platform channel,
or through the interfaces defined in a platform
interface package.
<topic_end>
<topic_start>
add support for platforms in an existing plugin project
to add support for specific platforms to an
existing plugin project, run flutter create with
the --template=plugin flag again in the project directory.
for example, to add web support in an existing plugin, run:
if this command displays a message about updating the
pubspec.yaml file, follow the provided instructions.
<topic_end>
<topic_start>
dart platform implementations
in many cases, non-web platform implementations only use the
platform-specific implementation language, as shown above. however,
platform implementations can also use platform-specific dart as well.
info note
the examples below only apply to non-web platforms. web
plugin implementations are always written in dart, and use
pluginClass and fileName for their dart implementations
as shown above.
<topic_end>
<topic_start>
dart-only platform implementations
in some cases, some platforms can be
implemented entirely in dart (for example, using FFI).
for a dart-only platform implementation on a platform other than web,
replace the pluginClass in pubspec.yaml with a dartPluginClass.
here is the hello_windows example above modified for a
dart-only implementation:
in this version you would have no c++ windows code, and would instead
subclass the hello plugin’s dart platform interface class with a
HelloPluginWindows class that includes a static
registerWith() method. this method is called during startup,
and can be used to register the dart implementation:
<topic_end>
<topic_start>
hybrid platform implementations
platform implementations can also use both dart and a platform-specific
language. for example, a plugin could use a different platform channel
for each platform so that the channels can be customized per platform.
a hybrid implementation uses both of the registration systems
described above. here is the hello_windows example above modified for a
hybrid implementation:
the dart HelloPluginWindows class would use the registerWith()
shown above for dart-only implementations, while the c++ HelloPlugin
class would be the same as in a c++-only implementation.
<topic_end>
<topic_start>
testing your plugin
we encourage you test your plugin with automated tests
to ensure that functionality doesn’t regress
as you make changes to your code.
to learn more about testing your plugins,
check out testing plugins.
if you are writing tests for your flutter app
and plugins are causing crashes,
check out flutter in plugin tests.
<topic_end>
<topic_start>
developing FFI plugin packages
if you want to develop a package that calls into native APIs using
dart’s FFI, you need to develop an FFI plugin package.
both FFI plugin packages and (non-ffi) plugin packages support
bundling native code, but FFI plugin packages do not support
method channels and do include method channel registration code.
if you want to implement a plugin that uses both method channels
and FFI, use a (non-ffi) plugin. you can chose per platform to
use an FFI or (non-ffi) plugin.
FFI plugin packages were introduced in flutter 3.0, if you’re
targeting older flutter versions, you can use a (non-ffi) plugin.
<topic_end>
<topic_start>
step 1: create the package
to create a starter FFI plugin package,
use the --template=plugin_ffi flag with flutter create:
this creates an FFI plugin project in the hello
folder with the following specialized content:
lib: the dart code that defines the API of the plugin,
and which calls into the native code using dart:ffi.
src: the native source code, and a CMakeLists.txt
file for building that source code into a dynamic library.
platform folders (android, ios, windows, etc.): the
build files for building and bundling the native code
library with the platform application.
<topic_end>
<topic_start>
step 2: building and bundling native code
the pubspec.yaml specifies FFI plugins as follows:
this configuration invokes the native build
for the various target platforms and bundles
the binaries in flutter applications using these FFI plugins.
this can be combined with dartPluginClass,
such as when FFI is used for the
implementation of one platform in a federated plugin:
a plugin can have both FFI and method channels:
the native build systems that are invoked by FFI
(and method channels) plugins are: