How to migrate Flutter app to Web

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: fluttersparkline and cupertinoicons did not work, so we removed sparkline graph and replaced cupertino icons with material icons.

Following migrating guide,

  1. Modify pubspec.yaml
name: appbar_for_web
	description: A new Flutter project for Web.
	

	version: 1.0.0
	

	environment:
	  sdk: '>=2.3.0-dev.0.1 <3.0.0'
	

	dependencies:
	

	  flutter_web: any
	  flutter_web_ui: any
	

	dev_dependencies:
	  build_runner: ^1.4.0
	  build_web_compilers: ^2.0.0
	  pedantic: ^1.0.0
	

	dependency_overrides:
	  flutter_web:
	    git:
	      url: https://github.com/flutter/flutter_web
	      path: packages/flutter_web
	  flutter_web_ui:
	    git:
	      url: https://github.com/flutter/flutter_web
	      path: packages/flutter_web_ui

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

  1. Create web/index.html and web/main.dart
<!DOCTYPE html>
	<html lang="en">
	<head>
	  <meta charset="UTF-8">
	  <title></title>
	  <script defer src="main.dart.js" type="application/javascript"></script>
	</head>
	<body>
	</body>
	</html>

// Copyright 2019 The Chromium Authors. All rights reserved.
	// Use of this source code is governed by a BSD-style license that can be
	// found in the LICENSE file.
	import 'package:flutter_web_ui/ui.dart' as ui;
	import 'package:appbar_for_web/main.dart' as app;
	

	main() async {
	  await ui.webOnlyInitializePlatform();
	  app.main();
	}

  1. 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:

  1. 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.

[
	    {
	      "family": "MaterialIcons",
	      "fonts": [
	        {
	          "asset": "https://fonts.gstatic.com/s/materialicons/v42/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2"
	        }
	      ]
	    }
	]

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.

#flutter #google-chrome #web-development

How to migrate Flutter app to Web
2 Likes282.05 GEEK