sinew_models

pub package License Docs

The shapes every Sinew app shares: domain models, envelopes, Result and ViewState.

Sinew keeps three kinds of model apart, so a backend that renames a field touches one file. Domain is what the app needs, in business language. Remote is what the backend sends, shaped like its JSON. Entity is what is stored on the device. Remote and Entity each map to a Domain with toDomain().

Features

  • Domain, Remote and Entity, with value equality from props (no equatable dependency)
  • Envelope bases (ObjectEnvelope, ListEnvelope, PagedEnvelope, VoidEnvelope) that map any backend's body to one domain shape, and report a failure inside a 2xx body
  • Result<T>: a Success that carries the backend's message, or a typed Failure, read with when
  • ViewState<T>: Loading, Done and Failed, where Loading and Failed can keep the previous data
  • PagingDomain and PagingMetaDomain: one page shape for every backend

Getting started

dart pub add sinew_models

Usage

Map a response body to the domain, and read the outcome:

final class Order extends Domain {
  const Order(this.id, this.title);

  final int id;
  final String title;

  @override
  List<Object?> get props => [id, title];
}

final class OrderRemote extends Remote<Order> {
  const OrderRemote(this.id, this.title);

  final int id;
  final String title;

  @override
  Order toDomain() => Order(id, title);
}

final class OrderEnvelope extends ObjectEnvelope<OrderRemote, Order> {
  const OrderEnvelope(this.data);

  @override
  final OrderRemote? data;
}

final result = Result.success(const OrderEnvelope(OrderRemote(7, 'Two coffees')).toDomain());
final text = result.when(
  success: (order, message) => 'Loaded ${order.title}',
  failure: (error, stackTrace) => 'Failed: ${error.code}',
);

The full program is in example/main.dart.

Sinew is one architecture for Flutter and Kotlin, released as a set of packages at one version.

Documentation

License

Apache 2.0, see LICENSE.

Libraries

sinew_models
Domain, Remote, Entity, envelope bases, paging shapes, ViewState and Result.