How to migrate Flutter app to Web

How to migrate Flutter app to Web

We will migrate the code to Flutter for Web. With one single code, we can run on application on Mobile and 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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Google's Flutter 1.20 stable announced with new features - Navoki

Google has announced new flutter 1.20 stable with many improvements, and features, enabling flutter for Desktop and Web

Hire Flutter Developer

Are you looking for next-generation mobile applications to increase business benefits? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** is one of the most reputable mobile app development company, which provides Flutter...

Hire Dedicated Flutter Developer

Are you planning for creating a native-like cross-platform app for your venture? **[Hire Dedicated Flutter Developer](https://hourlydeveloper.io/hire-dedicated-flutter-app-developer/ "Hire Dedicated Flutter Developer")** to develop cross-platform...

What is Flutter and why you should learn it?

Flutter is an open-source UI toolkit for mobile developers, so they can use it to build native-looking Android and iOS applications from the same code base for both platforms. Flutter is also working to make Flutter apps for Web, PWA (progressive Web-App) and Desktop platform (Windows,macOS,Linux).

Flutter – A Boon to Mobile App Developers

Flutter app development is in trend; ever since flutter - the mobile app development framework, has been helping app developers to build amazing applications.