Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling mobile only plugins on Flutter web platform gracefully

I have noticed flutter plugins such as path_provider, firebase_messaging , google_fonts are not supported on Flutter web (which is still in beta). Could anyone provide some insight on how to use this plugins on the platforms they are supported, and also prevent them from breaking the app on unsupported platforms?

  • Is there any standard or documented way of handling plugins on unsupported platforms gracefully?
  • Can I exclude plugins from pubspec when building for unsupported platforms, may be using some kind of a wrapper?
  • What happens to Android or iOS native code that comes with some dart plugins when building for web?
like image 296
nipunasudha Avatar asked Oct 15 '22 04:10

nipunasudha


People also ask

Can Flutter be used for web and mobile?

Flutter transforms the app development process. Build, test, and deploy beautiful mobile, web, desktop, and embedded apps from a single codebase.

Is Flutter a good choice for Web development?

The fast development cycle and portability make the technology ideal for building minimum viable products (MVPs ) and prototyping: You can quickly test your business idea on different platforms. As for web development, Flutter can be a go-to solution for progressive web apps (PWAs) and single-page applications (SPAs).

How do you customize plugins in Flutter?

To modify a plugin, you need to Ctrl + click on the import line (for e.g. import 'package:dio/dio. dart';) ctrl + clicking on this line will open the source code for this plugin. You can edit the code there.


1 Answers

You can accomplish this with conditional imports. This answer provides an excellent method of doing this. The following are the essentials of that post:

The core idea is as follows.

  1. Create an abstract class to define the methods you will need to use in general.
  2. Create implementations specific to web and android dependencies which extends this abstract class.
  3. Create a stub which exposes a method to return the instance of this abstract implementation. This is only to keep the dart analysis tool happy.
  4. In the abstract class import this stub file along with the conditional imports specific for mobile and web. Then in its factory constructor return the instance of the specific implementation. This will be handled automatically by conditional import if written correctly.

This method allows for you to do these imports based on platform and applies to all packages that may not support every possible flutter platform(e.g. dart:html, dart:js, dart:js_util, dart:io). It seems like the best way of handling different platforms with the same codebase at the moment.

As far as I know, you can't conditionally exclude plugins from pubspec.yaml(I may of course be wrong), though this shouldn't be necessary with the conditional imports that I mentioned earlier on.

Any native Android or iOS code that's a part of plugins you use is simply not included when you build for web. It's exactly the same for Android and iOS individually. When building for Android, iOS code simply isn't considered when the app is built. Building a flutter app only compiles dart code. It doesn't do anything special with native code other than what building a native app would have done.

like image 68
Christopher Moore Avatar answered Oct 31 '22 13:10

Christopher Moore