text
stringlengths 1
474
|
|---|
the Solution Explorer.You can run the example app by right-clicking hello_example in
|
the Solution Explorer and selecting Set as Startup Project,
|
then pressing the run (▶) button. Important: After
|
making changes to plugin code, you must select
|
Build > Build Solution before running again, otherwise
|
an outdated copy of the built plugin will be run instead
|
of the latest version containing your changes.<topic_end>
|
<topic_start>Step 2g: Connect the API and the platform code
|
Finally, you need to connect the API written in Dart code with
|
the platform-specific implementations.
|
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:<topic_end>
|
<topic_start>
|
Step 3: Binding to native code
|
To use the native code, bindings in Dart are needed.To avoid writing these by hand,
|
they are generated from the header file
|
(src/hello.h) by package:ffigen.
|
Reference the ffigen docs for information
|
on how to install this package.Regenerate the bindings by running the following:<topic_end>
|
<topic_start>
|
Step 4: Invoking native code
|
Very short-running native functions can be directly
|
invoked from any isolate.
|
For an example, see sum in lib/hello.dart.Longer-running functions should be invoked on a
|
helper isolate to avoid dropping frames in
|
Flutter applications.
|
For an example, see sumAsync in lib/hello.dart.<topic_end>
|
<topic_start>
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.