How to achieve one code base for all platforms with flutter
Most of the developers working on the app development side may have faced these kinds of challenges before reading this article where they get a requirement to use a hybrid platform to develop the mobile app for all platforms including web and mobile OS as well.
So Looking at these sorts of problems we as developers had to look for certain frameworks where we get to develop the apps for all platforms with one codebase. If we get one then it can make our lives a lot easier with which we can achieve more in very less time and increase our revenue as well without hitting our head in the wall. (Hahaha)
Yes. Flutter has all those capabilities with which we can write codebase common for all platforms. Flutter is a new framework developed by Google which is getting matured with time as its community is expanding. With the expansion in progress, Flutter has support for now web and mobile apps collectively.
There are different plugins available in flutter’s pub package repository from where we can download different plugins, some of them are suited only for mobile OS platforms and some of them are suited for all the platforms. So now the challenge comes that if those plugins that we are using are for different OS versions then we may need to make code bases different for different platforms. No that's not a problem right now.
Luckily we have a parameter in flutter’s Foundation library which gives us the option to run different codes on the basis of platforms detected.
That variable is KIsweb.
This variable is really special whenever you have to execute different code basis on different platforms. In most of the scenarios, our code works for web and mobile OS as well with single code base for a small app but for larger codebases where our project is very big to handle there it will come a lot handy as it provides us the flexibility to run different codes depending upon the plugins availability. I will demonstrate with an example to you.
1. We all or maybe some of us might have used firebase in our projects some or most of the time. Firebase does have different plugins available to us some of them are only for web and some of them only work on the mobile OS. Like firebase_auth, firebase_core, cloud_firestore, etc. like plugins only works in mobile OS-related codes but if you run the similar code in the web you will get a platform exception so for these scenarios you might need a different plugin like firebase which is solely working for the web for different firebase services like authentication, database, storage, crashlytics and so on. So for these requirements, we might have to maintain different code bases if we are not aware of complete options available to us.
So if we use the KIsWeb parameter from the Foundation library of flutter. We can load different instances of these classes at runtime. Where ever we are creating instances of firebase services related classes there we can add a check for kIsWeb field and create different instances at runtime and we can execute different codebase at runtime. and In this way, we can maintain single code base for all platforms. These were a very simple and common example to the problem that most of us would have come across.
2. The second problem can be related to geolocator plugin which is used to perform the reverse encoding support for location parameters in our OS apps. This plugin also does not have support for the web so at this point also Kisweb variable will come handy in maintaining single codebase functionality.
These were some of the situations or problems I came across and so I thought of writing the articles for those who need help. Please do send in your queries if any in the development of the app in flutter or angular or any other discussion on apps. I will be very happy to help you.