How to build Flutter app to run on Desktop

How to build Flutter app to run on Desktop

The technical preview for Flutter Web was released, and a live demonstration showed how Flutter apps can run on Desktop environments,

Google I/O 2019 was about innovation, and for Flutter it was about going beyond mobile apps. The technical preview for Flutter Web was released, and a live demonstration showed how Flutter apps can run on Desktop environments, like Chrome OS, Linux, Mac OS, or Windows. In this article we will go through the process of running a new or an existing Flutter application on a Desktop environment.

Let’s get going.

In my attempts, I have found that there are multiple ways to do it, so for the sake of simplicity, let’s go with the easiest one.

Prerequisites

For Flutter to run on Desktop, we must be on the master channel, with the latest release. To ensure the same, fire up a terminal and run the following commands.

flutter channel master
flutter upgrade

Now if we run

flutter doctor 

we should see an output like the one shown below (as of 17th May, 2019)

This is image title

Now with that out of our way, we can see that our even though I am on Ubuntu, the Linux system is not being displayed as a connected device, capable of running Flutter. This is because by default Flutter does not not have desktop support enabled. We can do so by setting the environment variable ENABLE_FLUTTER_DESKTOP=true.

To do this, in a terminal key in the appropriate command:

On macOS or Linux:

export ENABLE_FLUTTER_DESKTOP=true

On Windows:

PowerShell:

$env:ENABLE_FLUTTER_DESKTOP="true"

CMD:

set ENABLE_FLUTTER_DESKTOP=true

Please note that this sets the environment variable for the current terminal session, hence we will be doing all the future steps in this terminal itself.

Now, let’s run the following command to ensure that our system shows up.

flutter devices

And in the output, I now see that Linux is connected and available.

This is image title

Manual Configuration

Flutter for Desktop is still an experimental feature, which means that there is no tooling support for Flutter, also the flutter create command does not currently support creating a new desktop application. So the only option is to manually configure the system specific files. Thankfully for us, the Flutter team at Google already did that for us.

Go ahead and run this in the terminal:

git clone https://github.com/google/flutter-desktop-embedding.git
cd flutter-desktop-embedding

The example directory is a Flutter application which has all the necessary build scripts, which will be required to run Flutter on MacOS, Windows, and Linux. If we open the example folder in VS Code we will be able to see something like this:

This is image title

Notice the linux, macos, and windows folders

All that is left to do, is run the following command from inside the example folder.

flutter packages get

Just one last step before we go ahead and run our app. The desktop system specific build tools are not download by default, and even though Flutter will download the same when we first run our app, I want to ensure that we have it beforehand. To download the same, run:

This is image title

Make sure to replace linux with windows or macos, depending on your system.

Congratulations! We are now ready to run our Flutter app as a Desktop application.

Let’s just run the app first, and we will go over the code boilerplate code after that. In the terminal window, execute:

flutter run

The terminal output should be something like this:

This is image title

And the ever so beautiful desktop application, will look something like this:

This is image title

Flutter App Running on Desktop

Let’s go through the boilerplate code now:

// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:io' show Platform;

import 'package:flutter/foundation.dart'
    show debugDefaultTargetPlatformOverride;
import 'package:flutter/material.dart';

void main() {
  // See https://github.com/flutter/flutter/wiki/Desktop-shells#target-platform-override
  debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;

  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        // See https://github.com/flutter/flutter/wiki/Desktop-shells#fonts
        fontFamily: 'Roboto',
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Flutter_on_Desktop.dart

Most of this code is the usual Flutter stuff, but there are some lines that are different.

import ‘dart:io’ show Platform;
import ‘package:flutter/foundation.dart’ show debugDefaultTargetPlatformOverride;

and

debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;

This code provides a way to override the default target platform. This can be used based on the requirements of the application. To know more about the same, click here.

Running Existing Apps on Desktop

Now that we have the necessary config files and scripts. We can run any of our existing apps on desktop.

To do that there are two ways:

  1. We can copy the system specific folder (linux, macos, or windows) from the example directory to our existing project directory. (not recommended)
  2. We can replace the lib folder inside the example directory with our existing code, and replace the pubspec.yaml file, with our existing one. (recommended)

I would recommend going with the second method, because a lot of times, I have seen that the 1st one doesn’t work. I have an app called Cryptex, and when I ran it on desktop, here’s what I had.

This is image title

Cryptex running on Ubuntu 18.04

This is image title

Needless to say the Copy, and Share buttons didn’t work because they will need platform specific customisation. But all the core Flutter features worked like a charm. ❤

Flutter is actively advancing towards the Web and Desktop platforms, but theses are early days. I can’t wait to use Flutter on a production app for mobile, web and desktop in the near future. Flutter on every screen, what’s more amazing that that, right!?

flutter programming

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

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

Complete SQLite CRUD Operations in Flutter

Now a days almost all application have to have some kind of data storage. Application without collaboration with other users will make use of local storage db – SQLite. In this tutorial, we are going to cover all CRUD operations in Flutter with SQLite.

Adobe XD plugin for Flutter with CodePen Tutorial

Recently Adobe XD releases a new version of the plugin that you can use to export designs directly into flutter widgets or screens.

Flutter - How to Add AdMob Real Ads in Flutter App | Flutter AdMob Tutorial

Hello Whats is up Everyone So, Today I am going to show u How to Add Admob Real ads in Flutter apps which are very Easy Implement After watching this video u...