Welcome aboard, developers! In this exciting journey, we will delve into the fascinating world of Progressive Web Applications (PWAs) and uncover the power of Flutter in building these web wonders.
PWAs are the latest buzz in app development. These web applications masquerade as native apps and offer many advantages, such as working offline and providing engaging user experiences. With the right blend of mobile apps' versatility and websites' connectivity, PWAs command the best of both worlds.
Combine the prowess of PWAs with the flexibility of Flutter. This Google-developed, open-source technology is an ace in cross-platform app development. It allows you to craft beautiful, high-performing applications from a single codebase for mobile, web, and even desktop. Google announced last year that Flutter's web support is now stable, making it a gamut of possibilities for developers targeting multiple platforms.
In this blog post, we will embark on a trip to build a Progressive Web App (PWA) with Flutter. Throughout this odyssey, we will anchor at different stages of the app development process to explore the surroundings and learn new skills.
Progressive Web Applications (PWAs) are next-gen mobile applications that offer a hybrid cross between regular web browsers and mobile apps. They provide an immersive, full-screen experience on the user's device without the hassle of a structured approval process. PWAs can be easily stored on the home screen, push notifications, and even work offline while being highly user-friendly.
Flutter, a UI toolkit from Google, is extensively used for creating compelling mobile, web, and desktop applications from a single codebase. Its breakthrough in cross-platform app development has made it a popular choice among developers worldwide. Creating a PWA with Flutter imparts a native-like user experience, significantly enhances cross-platform compatibility, and substantially reduces development time.
Looking at Flutter vs. PWA, we see that they serve different purposes in the digital world. While Flutter is a framework for building cross-platform apps, PWA is more of an implementation approach to creating mobile apps that work well with web technologies. But the amalgamation of both produces a result that's nothing short of amazing.
Google's substantial support for PWAs in Flutter breathes fresh life into cross-platform app development. It combines the characteristics and benefits of native and web apps, fitting together like pieces of a puzzle to form a sophisticated whole.
Before developing our PWA, we must ensure our toolkit is ready. Here, our toolkit refers to the Flutter environment.
Firstly, you need to install the Flutter SDK. Visit the official Flutter installation guide and follow the detailed steps mentioned there. It would be best if you had an editor that supports Flutter. Visual Studio Code, Android Studio, and IntelliJ are excellent picks that offer robust Flutter and Dart plugins.
Ensure everything runs correctly once Flutter is set up and your editor is installed. Flutter Doctor is a handy command that checks your environment and displays a report of the status of your Flutter installation:
1flutter doctor
The command checks your system and lets you know if there are any dependencies you need to install to use Flutter. The objective is to create a fully functional Flutter environment ready for action.
A new Flutter project can be created by running the simple command flutter create [project_name].
Here’s a quick guide to set up a new Flutter project:
Fire up your terminal, navigate to the directory where you want to store your project, and run:
1flutter create flutter_pwa
This command creates a new Flutter project named flutter_pwa. The project directory will have the following structure:
1flutter_pwa/ 2├── android 3├── ios 4├── lib 5│ └── main.dart 6├── test 7├── web 8├── pubspec.yaml 9└── README.md
Among these, the most important file is lib/main.dart, our main entry file. By default, Flutter provides a sample application counter to start with. Our starting point will be slightly different since we aim to create a PWA.
Let's continue to the next step after creating the project and understanding its structure.
Now that we have reached this far, let's begin our coding voyage into the Flutter universe. Our primary objective is to convert our existing Flutter project into a progressive web app (PWA).
First, we will rewrite our lib/main.dart file with a simple MaterialApp with an AppBar. It would look something like this:
1import 'package:flutter/material.dart'; 2 3void main() { 4 runApp(MyApp()); 5} 6 7class MyApp extends StatelessWidget { 8 MyApp({Key key}) : super(key: key); 9 10 @override 11 Widget build(BuildContext context) { 12 return MaterialApp( 13 title: 'Flutter PWA Demo', 14 theme: ThemeData( 15 primarySwatch: Colors.blue, 16 ), 17 home: MyHomePage(), 18 ); 19 } 20} 21 22class MyHomePage extends StatelessWidget { 23 MyHomePage({Key key}) : super(key: key); 24 25 @override 26 Widget build(BuildContext context) { 27 return Scaffold( 28 appBar: AppBar( 29 title: Text('Building PWAs with Flutter'), 30 ), 31 body: Center( 32 child: Column( 33 mainAxisAlignment: MainAxisAlignment.center, 34 children: <Widget>[ 35 Text( 36 'Welcome to Flutter PWA Development', 37 ), 38 ], 39 ), 40 ), 41 ); 42 } 43}
After finishing up with the code, you will need to navigate into the project's root directory and run the app in Chrome by executing the following command:
1flutter run -d chrome
Flutter creates a local server hosting your web app by running the above command. Open your browser and navigate to localhost:8080 to see your application.
Having a PWA with Flutter goes beyond running your application in a web browser. Key PWA features include the ability to install the app on your device and have it run offline. Service Workers are instrumental in enabling these features.
Flutter automatically generates a service worker file, flutter_service_worker.js, in the build process. This file is responsible for managing caching for offline use. To fine-tune the caching strategies, you should edit the web/manifest.json and web/index.html files generated by the flutter create command.
The web/manifest.json file contains your application's Web App Manifest. It includes information on the program (such as its name, creator, icon, and description) in a JSON text file, which is required for the device to install web applications on the home screen.
Here's a basic example of a manifest.json file:
1{ 2 "name": "Flutter PWA Demo", 3 "short_name": "Demo", 4 "start_url": "./index.html", 5 "display": "standalone", 6 "background_color": "#0175C2", 7 "theme_color": "#0175C2", 8 "description": "A Flutter PWA demo application.", 9 "orientation": "portrait-primary", 10 "icons": [ 11 { 12 "src": "icons/Icon-192.png", 13 "sizes": "192x192", 14 "type": "image/png" 15 }, 16 { 17 "src": "icons/Icon-512.png", 18 "sizes": "512x512", 19 "type": "image/png" 20 } 21 ] 22}
The web/index.html file contains an app's HTML template. Note that there's an essential line in this file to make sure the service worker registers correctly:
1<script> 2 if ('serviceWorker' in navigator) { 3 window.addEventListener('flutter-first-frame', function () { 4 navigator.serviceWorker.register('/flutter_service_worker.js?v=<timestamp>'); // replace '<timestamp>' with your latest version/timestamp 5 }); 6 } 7</script>
Once you've made all these changes, run the following to create a release build suitable for hosting on a web server:
1flutter build web
You've successfully enhanced the PWA to work offline and have it installable on the device. The next section will walk through testing and debugging the app.
Testing and debugging are integral aspects of app development. Let's explore how we can check if the PWA works perfectly.
To confirm our Flutter web app is indeed a PWA, we need to verify two things:
The app must run offline.
The app can be installed on the device.
For this, we can use Google Chrome's built-in developer tools.
First, we navigate to the "Application" tab, then to the "Service Workers" from the left-hand menu. Here, we can see that the service worker is installed and running. We can also test whether our PWA is functioning offline by checking the "offline" box.
Create a release build of your app in Chrome to test the functionalities:
1flutter run -d chrome --release
To install your PWA on your device, you can find an "+" icon on the right-hand side of the URL bar on top. Clicking this icon installs your PWA on your device. If clicking the "+" does not do anything or does not appear, it suggests there's some error in your PWA.
Debugging in Chrome Developer Tools significantly helps to locate and fix unexpected behavior in your code.
Once satisfied with your PWA, the final step is deploying it on a server. Flutter does not specify a preferred method for deploying your web app, allowing you to deploy your app the way you would any static website.
Here are the general steps to follow:
Build the app using Flutter Build Web.
A build/web directory gets created, containing your app's built version, including HTML, JS, CSS, fonts, and other assets.
Copy the output directory files, and follow the deployment instructions for your selected hosting option. ( Here are some examples of hosting solutions: Firebase, GitHub Pages, or a self-hosted server with Nginx, Apache, etc.)
That’s it! Congrats on deploying your first PWA with Flutter. Your users can now enjoy a web experience that feels like a native app.
Reflecting on our journey, combining Flutter and PWA technologies has given us many advantages. Flutter empowers developers to tailor apps faster with a single codebase for iOS, Android, and the web. These apps work seamlessly across different platforms and look and feel beautiful, just like native apps.
Moreover, PWAs combine the best of web and mobile apps to deliver user-friendly solutions. Fast-loading Flutter PWAs that can work offline, provide push notifications, and be installed directly from the website provide a native-like user experience.
As Google announced Flutter's stable web support, the synergy between PWA and Flutter is set to skyrocket. With these powerful technologies in your toolkit, you have equipped yourself to build more potent, multi-experience apps quicker than before.
Building a PWA with Flutter is a seamless and straightforward process that leverages the best of web and mobile app development to deliver immersive and high-performance apps. Flutter’s incredible capability for cross-platform development and the versatile nature of PWAs simplify and amplify the process of creating dynamic apps, thereby revolutionizing the dynamic of cross-platform app development.
As we continue to immerse ourselves in the ever-changing world of technology, leveraging cutting-edge technologies like Flutter for PWA development will undoubtedly be the pathway to building smarter, efficient, and impactful web solutions.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.