brick_rest 1.0.0+3 copy "brick_rest: ^1.0.0+3" to clipboard
brick_rest: ^1.0.0+3 copied to clipboard

outdated

RESTful API connector for Brick, a data persistence library. Includes annotations, adapter, model, and provider.

example/example.dart

import 'package:brick_core/core.dart';
import 'package:brick_rest/rest.dart';

/// This class and code is always generated.
/// It is included here as an illustration.
/// Rest adapters are generated by domains that utilize the brick_rest_generators package,
/// such as brick_offline_first_with_rest_build
class UserAdapter extends RestAdapter<User> {
  @override
  final fromKey = 'users';
  @override
  final toKey = 'user';

  @override
  Future<User> fromRest(data, {provider, repository}) async {
    return User(
      name: data['name'],
    );
  }

  @override
  Future<Map<String, dynamic>> toRest(instance, {provider, repository}) async {
    return {
      'name': instance.name,
    };
  }

  @override
  String restEndpoint({query, instance}) => '/users';
}

/// This value is always generated.
/// It is included here as an illustration.
/// Import it from `lib/app/brick.g.dart` in your application.
final dictionary = RestModelDictionary({
  User: UserAdapter(),
});

/// A model is unique to the end implementation (e.g. a Flutter app)
class User extends RestModel {
  final String name;

  User({
    this.name,
  });
}

class MyRepository extends SingleProviderRepository<RestModel> {
  MyRepository(String baseApiUrl)
      : super(
          RestProvider(
            baseApiUrl,
            modelDictionary: dictionary,
          ),
        );
}

void main() async {
  final repository = MyRepository('http://localhost:8080');

  final users = await repository.get<User>();
  print(users);
}
1
likes
0
pub points
53%
popularity

Publisher

unverified uploader

RESTful API connector for Brick, a data persistence library. Includes annotations, adapter, model, and provider.

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

brick_core, http, logging, meta

More

Packages that depend on brick_rest