Configure the FlutnetBridge mode

Based on your development preferences you should use a specific FlutnetBridge mode.
In this example we will use the WebSocket mode by adding this line of code in the main.dart file. This configuration allow to develop Xamarin and Flutter in complete isolation.
MyFirstApp.Android
App.cs
// ...imports
namespace MyFirstApp
{
public static class App
{
// ...
public static void ConfigureFlutnetBridge(FlutterEngine engine)
{
try
{
#if DEBUG
_bridge = new FlutnetBridge(engine, AppCtx, FlutnetBridgeMode.WebSocket);
#else
_bridge = new FlutnetBridge(engine, AppContext);
#endif
}
catch (Exception e)
{
// Handle bridge errors
}
}
// ....
}
}
MyFirstApp.iOS
ViewController .cs
// ...imports
namespace MyFirstApp
{
public class ViewController : FlutterViewController
{
// ...
public override async void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
try
{
// Init the Flutnet Runtime and Services ....
#if DEBUG
_bridge = new FlutnetBridge(this.Engine, FlutnetBridgeMode.WebSocket);
#else
_bridge = new FlutnetBridge(this.Engine);
#endif
_initialized = true;
}
catch (Exception e)
{
// Handle bridge errors
}
}
// ....
}
}
my_first_app
main.dart
// Import MyFirstService class from my_first_app_bridge package: the namespace is converted in "Dart format"
import 'package:my_first_app_bridge/my_first_app/service_library/my_first_service.dart';
// Import FlutnetBridge class (auto-generated)
import 'package:my_first_app_bridge/flutnet_bridge.dart';
void main() {
// Configure the bridge mode
FlutnetBridgeConfig.mode = FlutnetBridgeMode.WebSocket;
runApp(MyApp());
}
We chose WebSocket mode to gain the full benefit about the hot-reload feature from Flutter.
WebSocket vs PlatformChannel
Hot-reload will work even using PlatformChannel mode, with the difference that all the changes made in Flutter (during the debug session) will be lost when the Xamarin app restart (you should rebuild the project to apply your change permanently in the Xamarin app: this is slow).
Instead, using WebSocket mode, the two application "parts" (Xamarin and Flutter) can be developed fully separated, so this last inconvenient will not persists.
Now you can run both Xamarin (Android / iOS) and Flutter project simultaneously: follow the guide Debugging in WebSocket Configuration to start your project.