Skip to main content

sinew_models 1.0.0 copy "sinew_models: ^1.0.0" to clipboard
sinew_models: ^1.0.0 copied to clipboard

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

example/main.dart

// ignore_for_file: avoid_print

import 'package:sinew_models/sinew_models.dart';

// region models
/// What the app needs, in business language.
final class Order extends Domain {
  const Order(this.id, this.title);

  final int id;
  final String title;

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

  @override
  String toString() => 'Order($id, $title)';
}

/// What the backend sends, shaped like its JSON.
final class OrderRemote extends Remote<Order> {
  const OrderRemote(this.id, this.title);

  final int id;
  final String title;

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

/// A response body: one order under `data`.
final class OrderEnvelope extends ObjectEnvelope<OrderRemote, Order> {
  const OrderEnvelope(this.data);

  @override
  final OrderRemote? data;
}
// endregion

void main() {
  // The envelope maps the backend's shape to the domain.
  const body = OrderEnvelope(OrderRemote(7, 'Two coffees'));
  final Result<Order> result = Result.success(body.toDomain());

  // region result
  // A Result is a success or a failure, never both: read it with `when`.
  final text = result.when(
    success: (order, message) => 'Loaded $order',
    failure: (error, stackTrace) => 'Failed: ${error.code}',
  );
  print(text);
  // endregion

  // A page renders a ViewState. Loading and Failed can keep the previous data.
  final ViewState<Order> state = Done(result.requireData());
  print(state);
}
0
likes
160
points
428
downloads

Documentation

Documentation
API reference

Publisher

verified publishersrctool.com

Weekly Downloads

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

Repository (GitHub)
View/report issues
Contributing

Topics

#architecture #models #result #error-handling

License

Apache-2.0 (license)

Dependencies

meta

More

Packages that depend on sinew_models