4 ways Flutter makes mobile app development delightful

4 ways Flutter makes mobile app development delightful

Subscribe now

Get the highlights in your inbox every week.

I'm going to let you in on a secret: For years I hated mobile development. I wanted to like it—mobile was the future! It was cool! It was low-power! It was a way to connect with users whose first exposure to computers did not come from traditional desktop platforms! And yet… development was a slow, frustrating experience for me. Instead, I sequestered myself over in the entirely problem-free area of web development and mourned the disappearance of the HTML blink tag (kidding).

Then, I discovered Flutter, an open source mobile app SDK developed by Google that enables developers to use the same codebase to create mobile apps for iOS and Android.

Once I found Flutter, I found that mobile development could be joyful.

Yes, joyful.

How, you ask? To show you what I mean, let's walk through writing a very simple Flutter app that queries Stack Overflow. As the self-respecting open source developer that you are, I'm sure you want to keep on top of the questions people are asking about your software on Stack Overflow. This app allows you to search for questions about a specific topic.

Lightning-fast development cycle

Traditional compilers are trouble. You know how it goes: You hit "compile," and the next thing you know you're 10 tabs deep into cute kitten photos—and it's lunchtime. Fortunately, when I worked on the web, interpreters and Just In Time (JIT) compilers saved me from my kitten tendencies, making "edit, save, refresh" the name of the game.

Flutter takes this quick development idea one step further: "edit, save." Although it's not a web technology, you can see your changes on your mobile device's screen in less than a second, thanks to Flutter's hot reload.

Typically you get this fast development cycle by using fancy, dynamically typed scripting languages, with the downside of pushing errors to runtime rather than catching them beforehand at compile-time. The second common drawback is their performance is not as zippy as compiled languages. By using Dart as its programming language of choice, Flutter can sidestep both of these issues. Dart has a strong, sound type system that allows you to catch problems before demoing That Part of the Codebase With Less Than Ideal Test Coverage.

Given these features, Dart is uniquely suited to provide developers with great development and release experiences for Flutter.

Finally, Dart was designed to be easy to learn, so if you've worked with any C-style language like Java, C++, or JavaScript, it will feel familiar.

Cool features, such as Streams and Futures

Time to start coding! Our app will use the Stack Overflow API to look for questions about Flutter that need responses, so that you, the intrepid open source project owner that you are, can help your community by keeping them informed. The simplest way to get that information in Dart is with an asynchronous request:

{"items":[{"tags":["android","ios","flutter"],"owner":{"reputation":1,
...},
is_answered:false,"view_count":1337,"title":"How to make a pretty Flutter app?"
...}

In this code snippet, http.get is returning a Future<Request>, which means that the result will be available in the future of type (Http)Request. Even though we're making a round trip to the server, we don't need to pass in a callback; we can just use the await keyword to wait for a response. Flutter has FutureBuilder and StreamBuilder widgets to build corresponding UI components, given the results of a Future or a Stream.

A Stream is just like a Future, except that it can provide results asynchronously multiple times instead of just once. In our app, we'll create a Stream where we can listen for updates on our Stack Overflow questions of interest. Since the Stack Overflow API doesn't provide push notifications out of the box, we construct our own stream using a StreamController and add updated Stack Overflow information whenever we get it:

In refreshQuestions, we make a new call to the Stack Overflow API, then filter the results so we're only looking at questions that have not been answered. From those results, we pull out the titles of the questions to display them in our app.

Flutter conveniently provides a StreamBuilder widget that can automatically update what the user sees in the app based on the contents of a stream. In this case, we provide the input stream source (controller.stream) and display different results depending on whether we successfully received data or not (in this case building terribly exciting Text widgets). StreamBuilder also conveniently takes care of unsubscribing itself from the stream and cleaning up after itself.

The result of the code above. (I don't understand why no design school would accept me.)

One technology for both iOS and Android

"Don't Repeat Yourself" is a common software engineering mantra, yet the mobile development world seems to be in denial. All too often, companies spin up separate iOS and Android app teams, each of which needs to solve the same problems twice. With Flutter, you can write in Dart and deploy natively to both iOS and Android. Scrolling behavior, system fonts, and other fundamental interaction components automatically default for the platform you're using. At a higher level, Flutter provides Cupertino and Material Design widget libraries to get the look and feel users expect on their platform of choice.

In our Stack Overflow app, we want to have a "Get New Results" button to see if there are new questions that need our attention. We'll write a PlatformAdaptiveButton whose behavior depends on the platform we're running on:

Which, when pressed, requests updates from the Stack Overflow API. Flutter's roadmap calls for more built-in ways to have platform adaptive components in your code, so stay tuned.

A handful of other app development systems provides cross-platform functionality, too: React Native, Xamarin, and Ionic, to name a few. With React Native and Ionic, you develop in JavaScript, which has the potential for less type safety (and therefore more unwanted surprises at runtime), and the code is interpreted or JITed. With Xamarin, you get strong type-safety guarantees with C# and, depending on the target platform, the code is compiled to native, JITed, or run on a virtual machine. Flutter compiles down to native machine code on both iOS and Android, giving it predictable, speedy performance.

Customization

"But Emily," you say. "I work at an agency and I simply can't have all the apps I create look the same! I need them to look distinctive and add those stylish touches, like my signature, tasteful usage of Comic Sans!" Never fear, my aesthete friends. Flutter really shines in this area.

Because Flutter is drawing every pixel to the screen, everything is customizable. Don't like how that built-in CupertinoButton is behaving? Make a subclass and design it yourself. Think solid-color app bars are so passé? Write your own widget. In our Stack Overflow app, I wrote a custom app bar that has a custom font and a gradient color scheme to distinguish it from all those other boring app bars—and it's not even much code:

Intrigued?

There is so much more we can do! However, further customization is left as an exercise to the reader… or as an enticement for you to come to OSCON, where we'll be coding, from the ground up, an entirely different, even more useful, and beautiful app for open source enthusiasts.

Topics

About the author

Emily Fortuna - Emily Fortuna is a senior software engineer on the Dart team at Google. When not hacking on compilers and evangelizing the awesomeness of Flutter, she can be found working on improving fairness in machine learning or acting on the stage and screen. She is an avid member of the nerdy joke appreciation society.

Oleksandr, I think it very much depends on what you're trying to achieve. If you want to truly write code once, then don't do any platform specialization -- just create a consistent style for your app and stick with it -- it needn't be iOS or Android themed, just the brand of the app.

If you have a tweaks at the widget level that you want to specialize per-platform, then the technique I describe in this article can a reasonable approach -- just create a widget and wrap the customization within a specialized widget.

If you have much more extensive changes that you want to do, then dependency injection may be an approach worth investigating. Our team is currently busily working on creating better ways for developers to write apps for these different platforms to avoid a bunch of if-statements, so please stay tuned for more updates!

Footer

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat.

Opensource.com aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. Red Hat and the Red Hat logo are trademarks of Red Hat, Inc., registered in the United States and other countries.