Platform Services, Operations, Data & Events

 

 

 

In the previous section, we discussed about the Flutnet Architecture, and the shared NET standard project.

In this section we will discuss about what how you can expose your C# code to Flutter using specific attributes. Note that all the attributes explained in this section will be used by the $ flutnet pack command to export you code in the "bridge package".

So, what you can do?

 


Supported types

During the packaging procedure, Flutnet will convert all the primitive NET types in the corrisponding Dart type, used by flutter. 

 

Supported primitive types

 NET type  Dart type
 bool  bool
 string  String
 byte  int
 char  String
 short  int
 ushort  int
 int  int
 uint  int
 long  int
 ulong  int
 double  double
 float  double
 DateTime  DateTime

 

 

 

 

 

 

 

 

 

 

 

 

 

Supported collection types

 NET type  Dart type
 byte [ ]  Uint8List
 T [ ]  List< T >
 IDictionary< string, T >  Map< String, T >        
 ISet< T >  Set< T >
 IEnumerable< T >  List< T >

 

where T is a PlatformData type, or a supported primitive type.

 


PlatformData

A PlatformData is just a classic C# class object, that you want to transfer from Xamarin to Flutter and vice versa. 
 

 

Example 

I want to expose to flutter a User information, with name, last name, birthday and phone number, because we need to render it in the user interface.

 

So, i will create a User class (as data class), and its associated PlatformService.

User.cs

using Flutnet.ServiceModel;

namespace my.library
{
    [PlatformData]
    public class User
    {
        public string Name { get; set; }

        public string Lastname { get; set; }

        public DateTime Birthday { get; set; }

        public string PhoneNumber { get; set; }
    }
}

 

Now i can transfer this custom object between Xamarin and Flutter.

PlatformData not support Inheritance. You can apply this attribute to class that extends Object.

 


PlatformService & Operations

The concept behind a PlatformService is the idea to expose some native functionality, each one called FlutterOperation to Flutter. 

 

 

You only need to create a simple C# class or interface, declaring or implementing you desiderated methods and marking with appropriate attributes. If you need to manage some custom error, you can implement your custom PlatformOperationException.

 

Example

I want to provide a token to Flutter, in form of string value.

 

To do this i will create a TokenService class with the corrisponding GetToken() public method.

TokenService.cs

using Flutnet.ServiceModel;

namespace my.library
{
    [PlatformService]
    public class TokenService
    {
        [PlatformOperation]
        public string GetToken()
        {
            return "YOUR_TOKEN";
        }
    }
}

 

 

In case you need to implement different logics for the Android and iOS, you can expose to flutter an interface and implement it on your specific projects.

ITokenService.cs

using Flutnet.ServiceModel;

namespace my.library
{
    [PlatformService]
    public interface ITokenService
    {
        [PlatformOperation]
        public string GetToken();
    }
}

 


Main Thread Required

Based on the platform (Android or iOS), you may need to run some code on the Main Thread. Flutnet allow you to specify this requirement on the PlatformOperation attribute as properties.

MyService.cs

using Flutnet.ServiceModel;

namespace my.library
{
    [PlatformService]
    public class MyService
    {
        [PlatformOperation ( IosMainThreadRequired = true, AndroidMainThreadRequired = false ) ]
        public void MyNativeCall();
    }
}

 

In this example the operation MyNativeCall will be executed on the Main thread only for iOS. By default all the platform calls will be executed in background to improve UI performance.

 


Async support

Platform operations can be asyncronus, but only returning Task or Task<T>, where T is a supported type or PlatformData.

Note that void async is NOT supported. Use Task async instead.

 

 


PlatformEvents

You can expose your C# events, from your PlatformService to Flutter: You only need to mark your event with PlatformEvent attribute.

 

Supported delegates

The only supported delegate types are

 Supported delegate types
 EventHandler
 EventHandler < T >

 

Where T type extends EventArgs class and must be exported as PlatformData.

 

Example

We want expose to flutter 2 events from our class called MonitorService: 

  • the first, called OnStart will expose some detailed info, like when the event occurse (see MonitorEventArgs class)
  • the second OnStop with no details.

 

MonitorService.cs

using System;
using Flutnet.ServiceModel;

namespace my.library
{
    [PlatformService]
    public class MonitorService
    {
        [PlatformEvent]
        public event  EventHandler<MonitorEventArgsOnStart;

        // OnStop not have time information
        [PlatformEvent]
        public event  EventHandler OnStop;

       // Other code...
    }
}

 

MonitorEventArgs.cs

using System;
using Flutnet.ServiceModel;

namespace my.library
{
    [PlatformData]
    public class MonitorEventArgs : EventArgs
    {
        public Datetime  time;
    }
}