Sitemap

Migrate Flutter App to Web

4 min readMay 15, 2019

--

In the previous topic we created a Crypto app UI with Flutter. This time we will migrate the code to Flutter for Web and describe how to do it.

According to Google Blog, google announced that Flutter framework will be beyond mobile. For now, we can use Flutter not only to develop app for iOS or Android, but also develop application for Web, Desktop and Embedded.

For us, the ability to reach users on mobile, web, or desktop through the single code (or with little modification) will help us a lot in term of cost and time saving.

This topic, we will focus on Flutter for Web.

Architecture

Flutter Architecture
Flutter for Web Architecture

Overall, the Flutter for Web Architecture looks very similar to mobile architecture. The framework itself is written in Dart language. It provides abstractions for Theming, Widgets, Rendering, Animation, Painting, Gestures and UI Foundation. If you used to write code of Flutter app, you will be very familiar with these concepts.

Getting Start

Alright, let’s start with some basic.

First thing to do is installing the flutter_web built tools:

Ensure that the $HOME/.pub-cache/bin directory is in our path, to use webdev command from our terminal. In our case, we export PATH in .zshrc

Next, upgrade flutter

The flutter version should be higher than 1.5

Create a Project in Visual Studio Code

Use Flutter: New Web Project in Command Palette and create a new project.

After click enter, VS Code will create all necessary files and folder.

Next, run the following command for preview in browser.

This is different with Flutter app that we had to choose Start Debugging or Start Without Debugging.

That’s it, we can see our app is running on web browser.

Migrate Flutter app to web

In the end, we will have the result as following.

Currently, this Flutter for Web is still in development, we can see that plugins that we used before did not work yet. For example: flutter_sparkline and cupertino_icons did not work, so we removed sparkline graph and replaced cupertino icons with material icons.

Run on iOS simulator
Run on Web browser

Following migrating guide,

  1. Modify pubspec.yaml

2. Replace package:flutter to package:flutter_web and dart:ui to package:flutter_web_ui/ui.dart throughout application code in lib/ directory.

3. Create web/index.html and web/main.dart

3. Move assets folder inside web

By default, look like Flutter for Web looks for assets in web folder. So, we had to move it into here. But the good thing is, we didn’t have to define assets in pubspec.yaml anymore. Flutter for Web will automatically recognize it. We could access to the assets by:

4. FontManifest.json

Since cupertino icons did not work, we will use material icon by define in FontManfest.json (be careful about the spelling), and put the file inside web.

After all these modification, we run $ webdev serve — auto restart, and, yes, it’s running on web browser.

Conclusion

Otherwise, it’s required some modification on source codes and files. We can say that Flutter for Web worked like magic. With one single code, we can run on application on Mobile and Web.

Source code can be found on Github.

Thank you for reading.

References

--

--

Joe Sithixay Douangchak
Joe Sithixay Douangchak

Written by Joe Sithixay Douangchak

Chief Technology Officer @ New Concept Consulting Sole Co., Ltd. WE THINK BIG.

Responses (4)