Using Flutter plugins in a Flutnet App

 

Xamarin applications already support all of the most used Flutter plugins.

 

When you add a Flutter Plugin to you flutter module, you need to integrate corrisponding native part used by the plugin into your Xamarin application.

This is done differently depending the target platform:

  • for Android you need to add the corrisponding Nuget package related to the flutter plugin
  • for iOS you need to add the corrisponding ***.PluginInterop.iOS project with all the related plugin frameworks generated during the flutter module build process.

 

Remeber that each flutter plugin may required some application permissions, so you should configure correctly the app permission in your Xamarin Projects (Manifest for Android, and Info.plist for iOS).

In case you run the flutter module in STANDALONE mode, remember to configure this permissions in the ".android" and ".ios" directories (generated by flutter during the build process).

This folders will be resetted in case you perform a $ flutter clean commad, so the permissions (for the module in standalone) should be reconfigured every time you perform this operation.

 

See the full example in the tutorial section.

 

If you only need a "vanilla Flutter package" (like a Dart library), you are just ready to go: add it to the pubspec.yaml and write your code.

 

 


Wrappig Xamarin Libraries in a PlatformService

 

Before using a Flutter plugin you can check if the same feature is already implemented in a .NET standard xamarin library, on Nuget. 

One peculiar example is the most used flutter plugin: shared preference plugin, that allow flutter store data for both Android and iOS.

For Xamarin exists the Xamarin.Essentials package that provide the same functionality.

→ You just add it in the *.ServiceLibrary project of your Flutnet App solution.

Now you can support shared preference writing your Xamarin code in the share project with your own PlatformService.

 

SharedPreferenceService.cs

using Flutnet.ServiceModel;
using Xamarin.Essentials;

namespace MyFirstApp.ServiceLibrary
{
    // Service to handle shared preference in flutter
    [ PlatformService ]
    public class SharedPreferenceService
    {
        [ PlatformOperation ]
        public void SetString(string key, string value) => Preferences.Set(key, value);
        [ PlatformOperation ]
        public string GetString(string key, string defaultValue) => Preferences.Get(key, defaultValue);
        [ PlatformOperation ]
        public void SetInt(string key, int value) => Preferences.Set(key, value);
        [ PlatformOperation ]
        public int GetInt(string key, int defaultValue) => Preferences.Get(key, defaultValue);
    }
}

 

After you have build the project and executed the $ flutnet pack  command, you can use Xamarin SharedPreference in Flutter.

Awesome !! wink

 

Remember that Dart syntax not support methods overloading like C#, so if you try to export PlatformOperations with same name, the flutnet pack will fail generating a specific error.