Skip to main content

api_state 2.0.0 copy "api_state: ^2.0.0" to clipboard
api_state: ^2.0.0 copied to clipboard

A lightweight API state wrapper for Dart/Flutter state management.

example/api_state_example.dart

import 'package:api_state/api_state.dart';

class ServerFailure extends Failure {
  const ServerFailure(super.message, {super.code, super.stackTrace});
}

class LoadMoreFailure extends Failure {
  const LoadMoreFailure(super.message, {super.code, super.stackTrace});
}

void main() {
  ApiStatus<String> status = const ApiInitial();
  print(status); // ApiInitial()

  status = const ApiLoading();

  status = const ApiSuccess('Hello World');

  // Using the when() helper:
  final message = status.when(
    initialOrLoading: () => 'Loading...',
    success: (data) => 'Got: $data',
    failure: (error) => 'Error: ${error.message}',
  );
  print(message); // Got: Hello World

  // Using Dart 3 exhaustive switch:
  final label = switch (status) {
    ApiInitial() => 'idle',
    ApiLoading() => 'loading...',
    ApiSuccess(:final data) => 'data: $data',
    ApiFailure(:final error) => 'error: ${error.message}',
    ApiRefresh(:final data) => 'refreshing: $data',
    ApiLoadMore(:final data) => 'loading more: $data',
  };
  print(label); // data: Hello World

  ApiStatus<List<String>> usersState =
      const ApiLoadMore(['Ada', 'Grace']);
  usersState = usersState.toLoadMoreFailure(
    const LoadMoreFailure('Could not fetch the next page'),
  );

  print(usersState.data); // [Ada, Grace] — existing data is preserved
  print(usersState.error is LoadMoreFailure); // true — error subtype identifies it
}
1
likes
160
points
123
downloads

Documentation

API reference

Publisher

verified publisherramandeep.dev

Weekly Downloads

A lightweight API state wrapper for Dart/Flutter state management.

Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on api_state