stateful_service 6.0.1 copy "stateful_service: ^6.0.1" to clipboard
stateful_service: ^6.0.1 copied to clipboard

This package provides a stream-based way to represent stateful services in Dart, agnostic to any state management solution (BLoC, Riverpod, etc.).

example/example.dart

import 'package:stateful_service/stateful_service.dart';

class User {
  const User({required this.name});

  final String name;

  User withName(String newName) => User(name: newName);
}

class UserApi {
  Future<void> updateName(String newName) async => throw UnimplementedError();
}

class UserService extends StatefulService<User> {
  UserService({required super.initialState});

  final UserApi _api = UserApi();

  /// Updates the user's name.
  Future<void> updateName(String newName) => update((user) async {
        await _api.updateName(newName);
        return user.withName(newName);
      });

  /// Updates the user's name, updating the UI optimistically.
  Future<void> updateNameOptimistic(String newName) => streamUpdates((user, _) async* {
        yield user.withName(newName);
        await _api.updateName(newName);
      });
}
2
likes
140
points
885
downloads

Publisher

verified publisherwolverinebeach.net

Weekly Downloads

This package provides a stream-based way to represent stateful services in Dart, agnostic to any state management solution (BLoC, Riverpod, etc.).

Repository (GitHub)

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

equatable, logging, meta, rxdart, synchronized

More

Packages that depend on stateful_service