Return to site

Flutter for Java developers

inspired by Java Champion Ian Darwin

· java,mashup

From the talk: "Flutter is the new Java - or is it? "

We, as developers, are afloat and aflutter in a sea of new devices:

  • Android (hundreds of devices)
  • IOS (new models)
  • Web browser apps
  • Windows desktop apps
  • macOS desktop apps
  • Chromebook apps
  • And what else?

To avoid being adrift, you need to write code once and run it everywhere.

Who cares? Why cross-platform matters?

  • Cost of development
  • Time to market

The challenge is to know which cross-platform tools to use? Which way is better?

Let's just pick ONE that works!

Flutter, of course! But we need to talk.

Flutter:

  • is open-source
  • is designed to build beautiful apps easy
  • is partly under Google wing
  • supports a wide range of platforms

But, remember, there is no one-size-fits-all solution.

How we got here, and where to go: (a bit of history)

  1. 1995: Java goes online promising WORA(Write Once, Run Anywhere). It did so thanks to portable, documented Class file format. It provided Java Runtimes for all major platforms (Windows 95, OS2, Solaris, Macintosh...).
  2. A GUI toolkit came along with it: AWT for windowing.
  3. AWT had some limitations (inheritance of platforms bugs, platforms features harmonized downwards), so followed Swing UI toolkit. But that was not designed for mobile (AWT did not works, and Swing had too much overhead for mobile devices of that time).
  4. Android entered the Game with a Java GUI optimized for mobiles, and Google bought them.
  5. Then came the Hotspot VM in order to speed things up. (optimizes compilation of highly used methods)
  6. The next GUI entering the game was JavaFX with its ups and downs. (Its purpose was to replace Swing, it became open-source under the name of Gluon which can run on IOS/Android/Desktop. That makes JavaFX a competitor of Flutter.)

Other WORA schemes:

  • Web-based: PhoneGap (Cordova), ReactNative (JS+Reactive programming)
  • Microsoft Xamarin (C#)

Flutter delivers WORA

  • Dart is the programming language used in Flutter, Flutter being the SDK (toolkit) using Dart.

Let's take a look at Dart language !

Dart language:

  • Modern, concise, object-oriented + functional
  • In the "brace structure" family (C, C++, Java, C#, etc.)
  • Borrowings from other languages (Kotlin?)

Dart overview:

  • Concise syntax
  • Type inference
  • DRY
  • Multi-paradigm: unstructured, object-oriented, functional
  • Standalone begins at main()
  • Hello World program in Dart is just:
  • Running it is simple:

Dart source files & organization:

  • A standalone dart program has a 'bin' folder for the main program and 'lib' folder for included files.

* Flutter convention is to omit 'bin' folder, keep main in 'lib' folder: only one source directory

  • Source files should be named with lower_case_name.dart convention, e.g:

* main.dart - main program

* new_course.dart - NewCourse class and friends

  • ->No requirement for source file and classname to agree !
  • ->A source file may have multiple classes, e.g: course_page.dart:

Other naming conventions:

  • ->Variables are named with lower-camel-case, e.g.:

Visibility:

  • There are almost no-written-out setter/getter methods in Dart
  • Members are public by default
  • Members are made private by a leading underscore in the name
  • Members may be declared static with usual meaning: single allocation, use without instantiation

Getting stuff out:

  • Most basic output statement is "print"

* like e.g., System.out.println() in Java, print() in Python, etc.

Typing and variable inference

  • Everything is an object, even integers and booleans
  • Declare and/or initialize as in most languages

* int i = 42; // creates an int object

* i = 27; // creates a new int object

  • Built-in types are int bool (not boolean) String
  • Local variable types can be inferred using 'var' as in Java 10

* var x =42; // x is int

* var y =42.0 // y is double

  • Lists and Arrays are interchangeable
  • var ages = [25, 31, 27]; //list with length 3
  • Map peopleScores = {'Ian':25, 'Linda':31, 'Robin': 27};
  • Map

 peopleScores; // more careful definition

Dart Strings

  • Strings are entered with either quote, as long as consistent
  • var names=["Ian", 'Robin',"Jo"]; // single or double quotes
  • print(names.length);// prints "3"
  • print(names.runtimeType);// prints "List"

String interpolation

  • Dart strings can include variables' values with $
  • Obviates most uses of string concatenation or "String Builder" classes

Dart operators

  • Dart has most of the common operators in modern languages
  • <><=>=+-*/% - standard
  • <<>>|&^ - bitwise operators
  • ~ ==
  • Most of these can be overridden within a class ("operator overloading")

Dart languages keywords

  • Like Java, Dart has about 50 reserved words, many of whose meaning is "standard"
  • They cannot be used as identifiers

Const vs Final

  • C++ introduced 'const' keyword
  • Java removed it, provided 'final' instead
  • Dart allows both !
  • final means as in Java: assign only once
  • const means state is constant; must be compile time

Classes and interfaces

  • Not distinct as in Java
  • Every class defines implicit interface with same name
  • Can implement multiple interfaces:

Enums

  • Enum is a Dart feature for a list of constant values (much like Java)
  • enum toString includes class name, e.g.: OpsUnit.NA. This below does work:

will always throw exception. Why?

Two problems on code above:

->'return' inside a closure just returns from the closure (same as Java)

->enum toString includes class name, e.g.: OpsUnit.NA

This below does work:

Dart null-aware operators

* Common source of program errors and crashes

  • ?? is _if null_ operator

'expr1 ?? expr2' gives 'expr1' if not null, else 'expr2'

  • ??= is _null-aware assignment_

'v??=expr' gets 'v' assigned 'expr' only if 'v' is 'null'

  • x?.p is _null-aware access_

'x?.p' gives 'x.p' if 'x' is not null, else gives 'null'

  • x?.m() is _null-aware method call_

'x?m()' invokes 'm' only if 'x' is not null

  • Also has C/Java/etc "ternary operator" 'x ? true_expr:false_expr'

if x true, evaluate true_expr, else evaluate false-expr

Strong typing, the easy way:

  • Like Java, Dart is a strongly-typed language with some flexibility
  • Like JavaScript, and like Java 10, can use 'var' keyword to avoid having to give the type (Strong 'type inference' will assign correct type)
  • Unknown types have the type 'dynamic' - somewhat like 'Object' in Java

Dart coding conventions:

* These are made by 'experts' and those who created Dart itself

  • Read this over after class and internalize it

Dart/Flutter packages

  • Packages are like libraries

Dart extra packages

  • https://pub.dev links many add-on packages
  • Some are Dart-only, others Flutter-only, so read description
  • We will use e.g., url_launcher from here

There's more!

  • We've covered basics of Dart language
  • More topics to explore
  •  Generators/Iterators
  • See Dart language spec at ECMA
  • Explore Dart interactively at https://dartpad.dev

Let's get back to Flutter !

Flutter runs (almost) everywhere:

  • Android
  • iOS
  • MacOS desktop
  • Windows desktop
  • Web applications

Development tools

->NOTE that to build for any platform, you must have the native toolkit

  • Android: Android SDK (all major platforms)
  • iOS: Xcode (only runs on macOS!)
  • macOS: same
  • MS-Windows: VS Studio 2020 (not VSCode!)

~~>Can only build on Windows

  • Linux: clang, various others (only on Linux)

->Then you can pick and IDE

  • Android Studio (Free): Flutter has instant reload without losing data-- while coding.
  • Dartboard for Eclipse (lost cause)
  • Microsoft Visual Studio Code (VSCode) has plugins

One other thing..

  • You don't have to learn and use a separate language like XML to lay out your components -- it's all done in Dart code

There are a lot of librairies:

  • Database with 'sqlite': 'moor' lib

Design patterns

  • MVC (Model View Controller)
  • MVP (Model View Presenter)
  • MVVM (Model View ViewModel)
  • BLoC (Business Logic Components, everything stream-based)
broken image

Is there a dark side to Flutter?

  • Google has a veto on every decision on Flutter
  • Dart isn't 100% cross-platform? Mainly the three major platforms.
  • You have to learn:
  1. Dart language
  2. Flutter API
  3. A bit of yaml

Flutter: What's next for Flutter?

  • Follow twitter account: @FlutterDev

Conclusion:

->Flutter offers a solid path to cross-platform development

  • Single code base for all platforms
  • Portability handled: 

~~>CPU portability handled by Dart

~~>Operating system portability handled in libraries

->Not the only cross-platform solution

  • But one of the best

Your plan to evaluate Flutter for yourself:

  • Install: Install Android Studio
  • Build: Build the sample apps (Tweak them, break them, learn from them)
  • Build: Build a simple app on your own (Test it on multiple platforms)
  • Start: Then start building your real app -- in Flutter !

Resources: