How to share common code between multiple flutter engines? - android

I have a flutter added into my android host project.
I am currently having multiple flutter engines.
But there is no sharing of code between these engines.
Therefore method-channel calls are happening multiple times.
I have a single entry point for all the engines.
I tried using singleton in my flutter code, but that is also executed multiple times.
What would be a good solution to this.
I read 'IsolateGroup' can be used but I haven't implemented it yet so I don't have a reference.

To share common code between multiple Flutter engines, you can create a separate Dart package that contains the shared code and then import that package into each engine that needs to use it. This allows you to maintain a single source of truth for the shared code and easily update it in all places where it is used. You can create a Dart package using the pub package manager and publishing it to a package repository such as pub.dev. Once the package is published, you can import it into your Flutter app using the dependencies section of your pubspec.yaml file.

Related

Use same model classes (DTO) between flutter and the native code

By now we have 2 android apps, one invokes the other one (directional relation). The first passes data via intent, by the compiled and published to our nexus repository model, written in kotlin (serialized to json).
Now, we want to migrate the second (invoked) app to the flutter, and still have the shared model. My question is - is it possible to use kotlin DTOs to the flutter (so far I see that the method channel does not allow for custom classes), or even better - to compile dart package to jar, and use it in the native code? In the first case I think about something like typescript definition files used when you use ts and js in one project.

How to let the Android/iOS code of one Flutter plugin depend on that of another Flutter plugin?

Concrete example: Say I have an app my_app, Flutter plugin plugin_utility and Flutter plugin plugin_b.
The plugin_utility has some helper Android/iOS code. (For a concrete example, it has a sendLogFromNativeToDart(String) function that sends a log string from Android/iOS to Dart(Flutter) and manipulate them there.)
Thus, I want the Android/iOS code of plugin_b to depend on the respective Android/iOS code of plugin_utility.
However, how can I do it? Thanks for any suggestions!
Notice that, I am not talking about "let one flutter package to depend on another", which simply needs `pubspec

Flutter - Is there anything similar to sync adapter in flutter

I wanted to sync my local DB with my server. In Android we have sync Adapter that does the work and make life easier. I would wonder if we have something similar in Flutter or how can I do that in flutter.
Sync-adapters are core Android components. Flutter cannot directly provide a replacement for them. However, flutter does provide a mechanism to use platform specific APIs called flutter plugin. And although their are a lot of plugins already present, their is still a bit lack of plugins providing such core platform support.
Ideally, in an android application, the sync adapters are background services which can be triggered by the OS itself in a separate process, so the simplest way would be to do the following :
Create a separate sync-adapter inside the android directory of your flutter app. Make sure you correctly follow all the requirements for the sync-adapter to work. The documentation is in here.
Create a simple plugin-mechanism using this to invoke android APIs corresponding to the sync-adapter.
Also note that all this code - Android sync-adapter, flutter-plugin and your flutter-app, can simply reside in current code base. You are NOT REQUIRED to create a separate "plugin" to actually use the plugin.

How to log from platform specific module (Android) in Flutter

I am starting development of my first Flutter app and I would like to know if there is a way how to use breakpoints and logging in classes that are not written in Dart (either iOS or Android specific classes).
I didnt find any mention of platform specific logging in official docs
There are many plugin which can help you to put logs in flutter or if you want to make some custom logerUtils than you can also create your plugin and use in for you project it it very simple and you can find lots of refrence on the internet
also you can use this which provided by flutter as well
https://api.flutter.dev/flutter/foundation/debugPrint.html

Flutter platform specific dependencies

I want to build an app for Android, iOS and web from a single Codebase using Flutter. Since web does not support all Flutter plugins yet, I'll have to use alternatives that have dependencies (for example dart:html) which aren't available on Android and iOS.
How can I inject the right implementation depending on the platform on which the application runs, without loading unnecessary/unavailable packages?
This is possible using conditional imports. You can find an example of the syntax here: https://github.com/dart-lang/site-www/issues/1569. However, I can't seem to find the official documentation for this language feature.
import 'stub.dart'
if (dart.library.io) 'io.dart'
if (dart.library.html) 'html.dart';
Define methods in stub.dart throwing UnsupportedOperationException or something the like. It doesn't really matter since stub.dart isn't going to be imported anyway. Put the actual implementations in io.dart and html.dart, respectively. The signatures have to match those in stub.dart.
You probably only want to do this conditional import at a single point in your program so I highly recommend hiding everything behind a common interface defined somewhere else than in stub.dart (common.dart in this example). You can then import and implement common.dart in io.dart and html.dart and use conditional import to chose your implementation at your program root. This way everything else only needs to depend on common.dart.
You could put the common parts into a third hierarchy, then include that in your mobile and web hierarchies using local pubspec includes. I'm not sure how you'd publish that to pub if you wanted to share it, although if you're already sharing it, it'd just be three pub repos like you have locally.

Categories

Resources