Dart Flutter Article Tips

Handling Asynchronous Dependencies in Flutter & Dart

When working with Flutter and Dart, developers often face situations where a class depends on an async operation, such as fetching data from a network or establishing a database connection. This article presents five tips for handling async dependencies in Dart, illustrated through an example.
Plague Fox 2 min read
Handling Asynchronous Dependencies in Flutter & Dart
Photo by David Pupăză / Unsplash

In Flutter and Dart applications, it is common to encounter scenarios where a class depends on an asynchronous operation. For instance, a client or service may need to fetch data from a network, or a database may need to establish a connection before being utilized. There are various ways to handle these dependencies efficiently. This article will explore five different approaches to managing asynchronous dependencies in your Dart code.

For example, we have a base class for all examples like a:

abstract class ClientBase {
  abstract final FutureOr<Dependence> _dependence;

  FutureOr<Result> fetch() async {
    final dependence = await _dependence;
    /* Do some computation with dependence */

Passing the Dependency as a Parameter:

The first approach is to pass the dependency as a parameter to the method or constructor of your class. This lets you provide the dependency directly, ensuring it is available when needed. In the example code, the ClientPassed class receives the _dependence as a constructor parameter:

class ClientPassed extends ClientBase {

  final Dependence _dependence;

Preparing the Dependency in the Constructor:

In this approach, you initialize the dependency directly within the class's constructor. This ensures the dependency is ready for use after the class is instantiated. The ClientPrepared class demonstrates this method by initializing _dependence in the constructor:

class ClientPrepared extends ClientBase {
  ClientPrepared() : _dependence = $dependenceInitializer();

  final Future<Dependence> _dependence;

Using a Factory Method to Initialize the Dependency:

Another approach is to utilize a factory method to create an instance of the class with the dependency already initialized. This method involves awaiting the dependency initialization before returning the class instance. The ClientFactory class shows this technique with its initialize() method:

class ClientFactory extends ClientBase {
  static Future<ClientBase> initialize() async {
    final dependence = await $dependenceInitializer();
    return ClientFactory._(dependence);


  final Dependence _dependence;

Lazy Initialization of the Dependency:

In some cases, it is more efficient to initialize the dependency only when it is needed for the first time. This can be achieved through lazy initialization. The ClientLazy class in the example code achieves this by using the late keyword:

class ClientLazy extends ClientBase {

  late final Future<Dependence> _dependence = $dependenceInitializer();

Lazy Initialization for Pre-Null Safety:

You can implement a similar approach using nullable types for projects that have not yet migrated to null safety. The ClientLazyNullable class demonstrates this method by checking if the _dependence is null before initializing it:

class ClientLazyNullable extends ClientBase {

  Future<Dependence> get _dependence => _$dependence ??= $dependenceInitializer();
  Future<Dependence>? _$dependence;

Handling async dependencies in Flutter and Dart can be achieved in various ways. The five tips provided in this article – passing the dependency as a parameter, preparing the dependency in the constructor, using a factory static method, and using lazy initialization with or without null-safety – can help developers choose the most suitable approach for their specific use case.

More from Plague Fox
Linter, Analyzer & Dart Code Metrics
Dart Flutter Podcast Russian

Linter, Analyzer & Dart Code Metrics

Join us in this stream where we will take a detailed look at Dart and Flutter tools such as linter, analyzer, and Dart Code Metrics. We will discuss how these tools help improve code quality and enhance its readability, as well as how they can make your development more efficient.
Plague Fox

Plague Fox

Engineer by day, fox by night. Talks about Flutter & Dart.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Plague Fox.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.