It has been seven or eight months since I learned Flutter. At first, I studied Flutter while watching others’ demos, then I wrote and played Android demos, and then I tried to apply Flutter to my company’s projects. I have more or less gained some experience.

When we talk about Flutter, we often talk about what Flutter is, and we often bring up this picture:

This is the nature of Flutter when it updates the UI by state. Whether it’s using setState, or using frames, what we can’t get around is the state.

What’s the state? I think states are just enumerations, and as many states as you have in your interface, you should actually use enumeration states to receive them.

Let’s first discuss a scenario where a web request is required to enter a page. There will be loading (chrysanthemum transfer, network request), request success (successful display page with content or no internal blank page), and request failure (failed page).

In Swift I will write the enumeration as shown above

enum ResponseState {
    case loading
    case success(ResponseStateSuccess)
    case error
    
    enum ResponseStateSuccess {
        case hasContent
        case empty
    }
}
Copy the code

Kotlin needless to say, Swift has it all, and passing in enums is routine, and enums are written almost exactly like Swift.

Under Dart, THAT’s all I can do

enum ResponseState { 
  loading,
  successAndHasContent, 
  successAndEmpty, 
  error 
}
Copy the code

Which way to write more scientific, more logical thinking, self-evident.

Flutter uses Dart as a programming language to take advantage of both JIT and AOT, but Flutter is a reactive UI framework for state drawing, and its enumeration is so weak that Swift and Kotlin’s users can only sigh.

The lack of enumeration parameters doesn’t seem to be a major problem in development, which can be mitigated by the utility class Utils or enumeration classes (fortunately, Dart’s enumeration also extends methods). It’s a little upsetting to think that the Dart enumeration was stuttering when I was using state to update the page.

As for Swift and Kotlin enumerations, both are more in line with modern programming. Swift supports parameter taking, protocol compliance, inheritance with value types, etc.

You can’t blame the Dart entirely, since it was only launched in 2011.

The Flutter team is currently adding Null Safe to Dart. In fact, this is an optional type in Swift. Kotlin has a similar idea. It’s good that Dart is working on this.

Hopefully, enumerations will be enhanced sometime.