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?
Flutter transforms the app development process. Build, test, and deploy beautiful mobile, web, desktop, and embedded apps from a single codebase.
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).
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.
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.
- Create an abstract class to define the methods you will need to use in general.
- Create implementations specific to
web
andandroid
dependencies which extends this abstract class.- 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.
- In the abstract class import this stub file along with the conditional imports specific for
mobile
andweb
. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With