Skip to content

W-JAX 2022: "Flutter for Java Developers: Web, Mobile & Desktop Front-Ends from 1 Code Base?"

Published on · Updated on · 19 min read
W-JAX logo

Share

Summary

Yes, we could build front-ends for mobile, web, and desktop from one codebase with Flutter. I wrote a sample app to demonstrate it. But I only recommended Flutter on mobile:

  • Flutter on mobile gave us “2 apps for the price of 1”. Flutter code could use native libraries because it could call native code (and vice versa). It could also show both native screens and web views. So Flutter could essentially do everything native SDK apps did, but with a faster build & deploy experience. And we Java developers liked Flutter because its language, Dart, was a “simplified Java, tuned for UI development”.
  • Compared to frameworks like React/Angular/Vue, Flutter only had 4% of the job ads. Flutter web also only had the material design look & feel vs. hundreds or thousands of design systems, component libraries, and UI skins. And because Flutter couldn’t use JavaScript libraries, it just had a tiny number of libraries vs. hundreds of thousands of JavaScript libraries for React/Angular/Vue.
  • On the desktop, I thought a web application was fine instead for most cases. If you still wanted a desktop application: Flutter had only gone stable on Windows in February 2022, and on Mac and Linux in May 2022. On the plus side, Flutter could call into desktop libraries with C++. But the third-party widget sets for Windows & macOS look & feel were incomplete — and it was an open question whether they’d always be maintained going forward.

I couldn’t generally recommend the two Java front-end libraries, Jakarta Server Faces (JSF) and JavaFX:

  • JSF couldn’t build the single-page applications (SPAs) we got from frameworks like React, Angular & Vue. It was very unpopular with developers and unpopular with employers and hadn’t received any new features for five years (2017–2022).
  • JavaFX used a UI build tool, not the more modern UI-as-code approach. On mobile, it couldn’t produce a native look & feel and didn’t have access to all native features. It was unpopular with both developers and employers.

Here was my recommendation on which cross-platform framework to use on mobile, based on the knowledge of the developers:

  • Web developers: Use React Native
  • .NET developers: Use .NET MAUI
  • Java developers who absolutely needed to include one or more legacy Java libraries: Use JavaFX
  • Else: Use Flutter

My slides showed why I came to this conclusion.

Logistics

Conference

W-JAX Hybrid 2022 was a hybrid Java conference in Munich, Germany. It ran from November 7 through November 11, 2022.

Talk

My talk took place on November 8 at 15:30 in the “Atlanta” room — which fit like a glove, as my start-up Seldon Systems was based in Alpharetta, north of Atlanta, Georgia, in the US. 😌

Abstract

Users were increasingly accessing our Java applications with mobile devices. And on mobile devices, native apps were often easier to use and prettier than web apps. But developing two different, native applications for iOS and Android was usually too expensive.

Cross-platform frameworks promised salvation and enabled mobile, web, and desktop front-ends with one codebase. For Java developers, Google’s Flutter was the best option. But could Flutter really deliver on that promise?

I had developed native applications for iOS and Android with Flutter and put them in the app stores. Based on my project experience, I described how Flutter worked and compared to alternatives. And with a sample application, I demonstrated how mobile, web, and desktop front-ends could actually succeed with one codebase.

Who Made Me the Expert?

I’ve been a Java developer for 26+ years and still write code every day. As a Java news reporter for InfoQ, I knew what was going on in the Java world. On the QCon London 2022 & QCon Plus Spring 2022 program committees, I helped organize the Java and front-end tracks, so I knew what was going on in the industry, too.

I was neither affiliated with the projects I discussed nor selling books or training courses. I shared industry analysis and my project experiences to give you options for your next project. But in the end, you had to decide!

Historical note (as of November 2022): After the talk, viewers could give live feedback via Mentimeter on what I should change next time. That poll is no longer reachable today.

Slides

The slides were available as a PDF (7 MB). They had more content and more text than my talking slides.

Download slides (PDF)

Historical note (as of November 2022): Back then, the slides were also available in their original Keynote format (9.8 MB) — with less text, but animated and with speaker notes; that file is no longer available today.

Video

At the time of publication, a video was announced to appear behind a paywall.

Get Started With Flutter

My guide had a full write-up on getting started with Flutter.

Start with Flutter

Additional Talk Information

Cross-Platform Front-Ends

Web Framework Popularity

I said that React, Angular & Vue were the three most popular frameworks. I knew because for my monthly Java report, I measured the popularity of JVM languages, databases, back-end frameworks, and front-end frameworks. I measured popularity among employers through job ads from 62 countries. For developer popularity, I used online training students, Stack Overflow questions, and Google searches.

Employers: Job Ads. The Indeed job search was active in 62 countries representing 89% of the worldwide GDP in 2020. It demonstrated the willingness of organizations to pay for a technology — the strongest indicator of popularity in my mind. Angular was the baseline. With 0.6% and 0.4%, respectively, Thymeleaf and Vaadin were missing from the chart.

React won, Angular was second, Vue third. React leveled off a bit over the previous couple of months, leading Angular 1.4:1. After dipping in June, Vue was catching up again with Angular and sat at an all-time high. JSF was an order of magnitude behind Vue and had been at or around 4% of Flutter’s volume.

Developers: Google Searches. Google Trends demonstrated the initial interest in a technology over time. Thymeleaf wasn’t on the list because Google only allowed five search terms.

This link still produces the chart above. Here’s a version with Thymeleaf instead of Vaadin: Thymeleaf also flat-lined. React won, Angular was second, Vue third. React was nearly back at its July 2020 peak and led Angular 2.5:1. Angular had 75% of its July 2019 peak. Vue peaked earlier that year but remained at half of Angular’s volume. Google changed the way it counted, so all three frameworks got a boost in 2022. All other frameworks were insignificant against these three.

Students at Udemy. Udemy was one of the biggest online learning sites. It published the number of courses and the number of students (beyond a certain threshold). Angular was the baseline. The other frameworks didn’t cross the reporting threshold for students at Udemy (maybe around 100,000 students).

React won, Angular was second, and Vue was third. After slowly pulling away from Angular for a year and a half, React had just registered its first-ever dip against Flutter, though it still led Angular 1.33:1. Vue continued its much slower catch-up to Angular. Links to the courses: Angular, JSF, React, Thymeleaf, Vaadin, Vue.

Questions at Stack Overflow. Stack Overflow Trends showed which percentage of questions at Stack Overflow had a particular technology tag.

This link still produces the chart above. React won, Angular was second, Vue third. React had grown spectacularly since 2015. After briefly exceeding 6% of all questions at Stack Overflow, it was taking a breather. Angular’s four-year decline continued. Vue had declined for half a year and was back to late 2019 levels. JSF, Vaadin, and Thymeleaf barely registered against the “Big Three”.

Native Look & Feel?

The slides mentioned native look & feel and why it mattered — especially on mobile, where a vocal minority would complain if an iOS app looked like an Android one and vice versa.

Google’s own apps, like Google Maps or Google Mail, had used mostly Material Design UI elements on iOS for years. Not anymore: In order to “really make products feel great on Apple platforms”, Google was switching to native iOS UI elements.

If you didn’t use both iOS and Android regularly, you didn’t know how meaningfully different they were — even if you tried to make them look and feel as similar as possible!

Here was the “My Data” page from my app, on iOS (left) and Android (right):

Three differences stood out:

  1. The page heading was centered on iOS and left-aligned on Android.
  2. The “Edit” button was in the navigation bar on iOS and a floating action button on Android.
  3. When you could tap on a table row on iOS, it had a chevron on the very right. Android didn’t have such an indicator.

Forms were also different:

Some differences:

  1. The “Cancel” button had a text label on iOS and a symbol on Android.
  2. Fields had no label on iOS, just a placeholder when they were empty. Android had both a permanently visible label and a placeholder.
  3. Text fields on iOS had a “Clear” button. Android didn’t have such a button.

Native Cross-Platform Frameworks

More Frameworks. Building two separate, native apps for iOS and Android was very expensive. That’s why there were a ton of frameworks & tools to build one app and get two native mobile apps. My talk mentioned Flutter, React Native, Xamarin/.NET MAUI, and JavaFX. Here were a couple more:

  • Qt: This framework went back to the 90s. Nokia owned it at one point. It stood out because you could build front-ends for embedded devices, too.
  • Codename One: a Java-based framework for mobile, web & desktop — so it seemed to be a direct JavaFX competitor. I’d never heard of it before researching my talk. They claimed “Trusted by 40k+ developers”.
  • Volt MX: a low-code framework for building both the front-end and back-end of an application, with a visual designer and component libraries. JAX had a German talk about it.

Popularity. The popularity survey among developers was from Statista. The source for the app analysis chart was this report by Appfigures.

Employers: Job Ads. The Indeed job search was active in 62 countries representing 89% of the worldwide GDP in 2020. Flutter was the baseline.

React Native won, Flutter was second, Xamarin third, and JavaFX last. Flutter had been on a roll since April. React Native went from leading Flutter 2.5:1 down to 1.5:1, Xamarin from 56% to 36%. JavaFX remained steady at a low level — the initially high JavaFX numbers were a counting error of mine: they came from Japan and disappeared in October the previous year.

Developers: Courses Bought At Udemy. This time, React Native was the baseline.

Flutter won, React Native was second, Xamarin third, and JavaFX fourth. Slowly but surely, Flutter increased its lead over React Native, leading it 2:1 by then. Xamarin and JavaFX held steady against React Native. Links to the courses: Flutter, JavaFX, React Native, Xamarin.

Developers: Google Searches.

This link still produces the chart above. Flutter won, React Native was second, Xamarin third, and JavaFX fourth. Flutter had twice the search volume of React Native. Both had just hit their all-time high but lost ground in the previous few months. JavaFX and Xamarin had declined for five years.

Developers: Questions at Stack Overflow.

This link still produces the chart above. Flutter won, React Native was second. Flutter had more than twice the questions React Native had and showed more robust growth. Flutter was at an all-time high while React Native was slightly off its peak from the end of the previous year. JavaFX and Xamarin had declined for 4-5 years.

How Flutter Works

Will Google Kill Flutter?

The “Killed by Google” website was one way to check.

The Angular website said, as of May 11, 2022, “For web, mobile web, native mobile and native desktop.” The Flutter website had a showcase for Toyota using Flutter in their Linux infotainment systems. And a Canonical blog post discussed their use of Flutter in Ubuntu — the Flutter-based installer wasn’t available yet as of May 11, 2022.

Programming Language: Dart

A language tour of Dart, by Dart itself, and a Google Codelab called “Intro to Dart for Java Developers” helped with getting started.

Widgets

Widgets were code. Flutter used the so-called declarative programming model — the old way of doing things was called imperative programming, by contrast.

I had a guide to getting started with declarative development:

Start with Declarative Development

Flutter didn’t use native UI elements (such as text fields or buttons). Instead, it emulated them. See the guide for more details.

Widget Sets

I built a sample Flutter application with five different, native “look & feels” with one codebase: Web, iOS, Android, Windows, and Mac. I showed screenshots during the talk.

Check out the sample application

Code, Build & Deploy

Flutter Hot Reload made code changes go live in the device/simulator within a second. It was the main reason why working with Flutter could be such fun! I mentioned it in the talk — here’s a video demonstrating it.

Watch the video (YouTube)

My Flutter Experiences

My Project

The start-up I co-founded was called “Your Home in Good Hands”. At the time of the talk, it was in private beta; the service was due to launch publicly later in 2022. The cat-sitting service has since been live under Your Home in Good Hands (UK).

Flutter 👍🏻

Yes, Flutter on mobile worked. Flutter had some small issues at times. For instance, sometimes the Flutter app didn’t terminate, and you had to kill it manually.

When there was trouble, it usually came from Apple. For instance, I could no longer copy text from the clipboard into a Flutter app in the iOS simulator — I nearly knew my long password by heart! 😁 That started a couple of months before the talk, and it wasn’t clear when it would be fixed.

I wished there was more help from Flutter for showing native look & feel. But the Flutter team always said you should “customize Material UI”, not use native look & feel. Since I built my own widgets anyway to centralize configuration, I managed.

The Flutter team did listen, though. For instance, a couple of years earlier, the Flutter community had worried about too many bugs piling up, so the Flutter team hired external developers to fix bugs. They also ran quarterly user surveys and then made changes based on the feedback.

I wasn’t ready to fully share our app yet. We did have screenshots on our home page, but they were slightly out of date.

But I did build a prototype in 2019 which at least showed a Flutter app in motion. Although it was old by the time of the talk, it was still a decent example of what a native Flutter app could look like. It took me about six weeks and was my first Flutter project. I used Google’s cloud service Firebase for login, NoSQL database, and file storage. I also built my own back-end with Java, JHipster, Spring Boot, and Angular.

Watch the video (YouTube)

Native 👍🏻

The “Native Look & Feel” section above already showed how my app implemented a native look & feel and how that was different between iOS & Android.

Here was the iOS screen of visits. It showed my custom list tile, as Flutter’s iOS widget set didn’t have that — I built it as a custom widget, based on a plugin.

And here was a detail screen of a visit on iOS:

There were three noteworthy things:

  1. This was a so-called sliver app bar that was initially taller than usual. When you scrolled down the page, the app bar shrank to its normal size, and the “Loaded: 3m ago” text disappeared.
  2. This was a sliding segment control. Apple added it in iOS 13 in September 2019. It took a couple of months until Flutter added it to its widget set.
  3. These buttons were custom widgets of mine. They looked the same on Android.

Firebase 👍🏻

I also had a guide dedicated to getting started with Firebase.

Start with Firebase

Mobile Power

Mobile phones were very powerful. They had powerful CPUs and lots of local storage. So when developing for mobile, we should think of phones more like PCs, less like web browsers.

One way to harness that power was to load and save data in the background.

  • My app loaded a lot of data in the background. So when the user hit many screens, there was no wait like there often was on web pages — the data was already there! But then my app immediately refreshed the data to make sure the user saw the latest data. This also helped when the phone was offline: the data was already there.
  • For many operations, my app also saved in the background. So the app stored the data locally and returned control to the user. It then sent the data to my back-end in the background. Again, this helped when the phone was offline: the data remained on the phone until the user was online again. Now, saving in the background only worked when we didn’t expect errors from the back-end (like form validation errors): we couldn’t show errors easily because the user had already moved on to other screens. And saving in the background could make our apps more complicated because we might have to “manually merge” our locally stored data into the data we loaded from the server.

Storing data locally and loading & saving data in the background was easy with Flutter. But we could also do that in a web application — more difficult and more restricted, but doable.

I knew because at the end of 2019, I built such a web application: a so-called progressive web app (PWA). A PWA used the “Service Worker” in a browser to install on your device and cache data. It took about four weeks, and it was my first PWA. I used Google Workbox for this but developed my own offline storage solution in the browser. I built my back-end with Java, JHipster, Spring Boot, and Angular.

Watch the video (YouTube)

One Source Of UI Truth

This is how I made sure my app UI didn’t get “out of sync”. Here are two examples of UIs that were “out of sync”.

This was the iPad app of the UK price comparison site Pricespy. That app wasn’t sure whether my list had seven items (top left) or just three (right):

And here was the macOS App Store. I had just added $75 to my account, which was now $113.38 (see the top right). But in the bottom left, it still said $38.38!

So I’d just given money to the most valuable company in the world, and they couldn’t figure out how much was in my account… embarrassing, if you asked me! It took a couple of hours until the App Store was back in sync.

So how did I fix this? There were many different ways. I went with a global data store. My guide to getting started with Flutter explained the use of Redux as a data store.

But that wasn’t enough. We also had to remove duplicates from our locally stored data. Here’s an example from my app:

The task home page (left) showed a chart with statistics about how many visits were already completed. The visit list (right side) showed a list of those visits. Initially, I stored this as two different data structures in my app: visit statistics and a list of visits. They could get out of sync, too: a user had just finished a visit, which triggered a refresh of the visit list. But the app didn’t refresh the visit statistics, so the task home page would show an incorrect, out-of-date chart.

So my solution was to get rid of storing the visit statistics altogether. Instead, the app calculated visit statistics for the chart on the left in real time, iterating over all the visits and checking their status. And yes, my app could do that because phones were powerful — see the previous section.

Be Consistent

My start-up offered a web app for managers and a mobile app for cat-sitters. So, arguably, consistency between them was nice-to-have.

But most managers were also cat-sitters. So they used both apps. This made consistency a necessity!

Here was the mobile app for cat-sitters:

And here was the web app for managers:

So how was this consistent — or how was it different?

  • Both apps used different navigation paradigms. The mobile app had a nav bar at the top (1) and a tab bar at the bottom (2). The web app had breadcrumbs (1) and a header (2).
  • Both apps shared the same colors. For instance, the mobile nav bar (1) & tab bar (2) and the web app breadcrumbs (1) all used the primary brand color. Navigation also used the same shade of blue (3).
  • The apps also used the same Google font for headings (4): Montserrat.
  • Finally, the apps shared symbols from Font Awesome: a walking person in this example (5).

Some good design books on the topic:

Back To Basics

Writing mobile apps was still writing software. So the same best practices applied here, too:

  • The best code is the one you don’t have to write. So reuse code somebody else wrote! In Flutter, this meant using a plugin.
  • DRY: Don’t repeat yourself. Instead of applying the same configuration code to widgets over and over, we should create our own widgets.
  • We should also structure our applications with best practices — that could mean horizontal layers and could mean vertical layers.

My guide to getting started with Flutter had an architecture section that explained my proposed use of components & layers.