data property

AsyncData<T>? data

The current data, or null if in loading/error.

This is safe to use, as Dart (will) have non-nullable types. As such reading data still forces to handle the loading/error cases by having to check data != null.

Why does AsyncValue<T>.data return AsyncData<T> instead of T?

The motivation behind this decision is to allow differentiating between:

  • There is a data, and it is null.

    // There is a data, and it is "null"
    AsyncValue<Configuration> configs = AsyncValue.data(null);
    
    print(configs.data); // AsyncValue(value: null)
    print(configs.data.value); // null
    
  • There is no data. AsyncValue is currently in loading/error state.

    // No data, currently loading
    AsyncValue<Configuration> configs = AsyncValue.loading();
    
    print(configs.data); // null, currently loading
    print(configs.data.value); // throws null exception
    

Implementation

AsyncData<T>? get data {
  return map(
    data: (data) => data,
    loading: (_) => null,
    error: (_) => null,
  );
}