Mindflow: Flutter deal with Future! in 6 Steps

With a Swift background, Flutter+Dart require a different thought-process. Future<T> is associated with async calls and it could be hard to re-route your brain in a one go. In this article I am sharing my experience on how the mindflow when dealing with Future<T> using SharePreferences as an example, from start to a 1-liner in 6 steps.

1. Direct mindflow:

This is how a method looks like with the consideration of existing of Future<T>, it doesn’t work since SharedPreferences.getInstance() returns Future<SharedPreferences> instead of SharedPreferences.

static int getCount(){

// This will return Future<SharedPreferences>, unwarp is needed.
final preferences = SharedPreferences.getInstance();

// Future<SharedPreferences> causes error
// because SharedPreferences has .getInt()
// but Future<SharedPreferences> does not have .getInt()
final value = preferences.getInt("attributeName");

return value;

2. Fix the Future!

To handle Future<SharedPreferences>, add async and await. But the value can be null!

// async is needed to use the keyword await
// async also require Future<int> instead of int
static Future<int> getCount() async {

// Keyword await "unwarp" Future<SharedPreferences> to SharedPreferences
final preferences = await SharedPreferences.getInstance();

// .getInt() works normally now,
// but null can be return if "attributeName" does not have a value
final value = preferences.getInt("attributeName");

return value;

3. Fix the null value!

Change the final to var for value to be validated:

// async is needed to use the keyword await
static Future<int> getCount() async {

// Good now
final preferences = await SharedPreferences.getInstance();

// Change final to var so value can be validated
var value = preferences.getInt("attributeName");

// Validating value
if (value == null){
value = 0;

return value;

4. Shorthand for if …

if … could be a bit blocky, we could replace it with (condition) ? (true) : (false):

static Future<int> getCount() async {

// Shortent this too
final pref = await SharedPreferences.getInstance();

// Replace if with (condition) ? (true) : (false)
var value = pref.getInt("attributeName") == null ? 0 : pref.getInt("attributeName");

return value;

5. Remove more

Future<T> can be stacked with (await Future<T>) to get T within the async method.

static Future<int> getCount() async {

// Remove this line too

// Replace it with (await Future<SharedPreferences>) to get SharedPreferences
var value = (await SharedPreferences.getInstance()).getInt("attributeName") == null ? 0 : (await SharedPreferences.getInstance()).getInt("attributeName");

return value;

6. One-liner!

1-liner, just return the value :

static Future<int> getCount() async {
return (await SharedPreferences.getInstance()).getInt("attributeName") == null ? 0 : (await SharedPreferences.getInstance()).getInt("attributeName");

[Review]  Apple Watch on the launch day. Why the white? Why the Cheap?

1. It just works!

I was surprised that Apple changed their mind and sent me an Apple Watch today instead of the expected June delivery. An SMS notification brought me the good news yesterday and the watch arrived today around 10:00. I literally unboxed it and put it on my wrist and rush to the bus stop. It just works!


2. SIRI-ous

I posted a few pictures of the unboxing on my Facebook, and i found that I am already getting notification and SMS about it. So where’s the keyboard!? SIRI is your only choice. When you tap reply, it give you a few default options with emoji and dictation. The default replies are “Ok”, “Yes”, “No”, “Call me later.”, etc. For those who think the default reply doesn’t encompass our language, it is clear that you are arguing emoji is kill the skill of painting. Anyway, dictation work, i suppose, better than what i thought. For all the message i have send so far, i just have to re-speak one of them. I only gave English a try, yet for Cantonese, Chinese and Japanese, I suppose they work the same. Bottomline, you can just dictation what you have to send.


3. Why the white?

I didn’t pre-order the Apple Watch on day one, instead took advantage and manage to get a session of try-on on day 3. I thought I was going to get black aluminium large size. The session make me understand 2 things:

  • Small size looks equally good compare to the large size in my case.
  • Leather strip is irresistible, but the price is not one that I wish to pay.
  • White look better with the leather strip I like.

The Apple employee show me the leather with white and black watch. To me the white one works better with the leather colour I like. The conclusion to this is, get the white aluminium and a third-party leather strip <3.

4. Why the cheap?

Obvious I can’t afford the gold watch, so it comes down to stainless steel and aluminium. As an iPhone Developer (now watch as well), it is always good to learn the new tech. However, the Apple Watch is at it first generation. The past experience of Mac and iPhone show that it is not wise to get the more expensive version for the first generation. Macbook Pro Retina is the completed product, so as the iPhone 4. An Apple Watch will worth the price if the technology is stable. All in all, the cheapest on for the first gen is a logical choice for a developer like myself.

5. Best features

To me it is all about integration, I believe in both integration and customisation. But you don’t build your own car, you buy one and drive, it should just work. Apple Watch just works, perhaps using iPhone, Mac OS X and Apple Watch is the reason. Best feature is integration and the health data collection. It works with the Health Apps and later on with Research Kit. That’s something that I want to develop on. It remind you to move a bit if you are at you seat for too long.

IMG_5595 2IMG_5591IMG_5593

6. Boxing pictures

I know, I know, here you go:

FullSizeRender 4FullSizeRender 5FullSizeRender 2FullSizeRender 7

IMG_5594IMG_5595 2

At last, the above is just one-man opinion

 Thank you for reading ❤