Menu

1 / 2

Flutter Bloc Async State: Stop Modeling Data With Nulls and Loading Flags

DEV Community: dart·Konstantin Voronov·5 months ago
#hKSF6116
#dev#state#error#enter#article
Reading 0:00
15s threshold

Flutter apps rarely start with complicated state.

At first, a Bloc or Cubit state often looks harmless:

class KidState { final Kid ? kid ; final bool isLoading ; final StateError ? error ;

const KidState ({ required this . kid , required this . isLoading , required this . error , }); }

Enter fullscreen mode

Exit fullscreen mode

This works until the screen becomes real.

You add initial loading. Then pull-to-refresh. Then a cached value. Then an empty state. Then an error that should not erase the old content. Then a retry. Then one more field with the same lifecycle.

Now the state is not simple anymore. It is implicit.

// What does this mean? kid != null && isLoading && error != null

Enter fullscreen mode

Exit fullscreen mode

Is the kid loaded? Is the screen refreshing? Did the refresh fail? Should the UI show the old kid, the error, or a full-screen loader?

The problem is not Bloc.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.