bloc_extras 1.0.0-a1
bloc_extras: ^1.0.0-a1 copied to clipboard

Extra voilerplates for boc package.

example/bloc_extras_example.dart

import 'package:bloc/bloc.dart';
import 'package:bloc_extras/bloc_extras.dart';

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';

part 'bloc_extras_example.g.dart';

Future<void> main() async {
  Bloc.observer = _Observer();
  final bloc = SimpleListBloc(_FakeListAdapter(BuiltList()));
  try {
    bloc.add(CreateItemEvent(
      _TestItem(1, 'First'),
    ));
    await Future.delayed(Duration(milliseconds: 10));
    bloc.add(LoadItemsEvent());
    await Future.delayed(Duration.zero);
    bloc.add(CreateItemEvent(
      _TestItem(1, 'First'),
    ));
    await Future.delayed(Duration.zero);
    bloc.add(CreateItemEvent(
      _TestItem(2, 'Second'),
    ));

    await Future.delayed(Duration.zero);
    bloc.add(DeleteItemEvent(
      _TestItem(1, 'First'),
    ));
  } catch (e) {
    print('Must load first!');
  } finally {
    await bloc.close();
  }
}

class _Observer extends BlocObserver {
  @override
  void onCreate(Cubit cubit) {
    print('Created ${cubit.runtimeType} instance');
    super.onCreate(cubit);
  }

  @override
  void onChange(Cubit cubit, Change change) {
    print('Changed cubit of type ${cubit.runtimeType}.');
    print('New state is as follows:');
    print(change.nextState);
    super.onChange(cubit, change);
  }

  @override
  void onClose(Cubit cubit) {
    print('Cubit of type ${cubit.runtimeType} has been closed.');
    super.onClose(cubit);
  }

  @override
  void onEvent(Bloc bloc, Object event) {
    print('Bloc ${bloc.runtimeType} got an event ${event.runtimeType}');
    super.onEvent(bloc, event);
  }

  @override
  void onError(Cubit cubit, Object error, StackTrace stackTrace) {
    print('Cubit ${cubit.runtimeType} got an erroor: ${error}');
    print('Stack trace:\n${stackTrace}');
    super.onError(cubit, error, stackTrace);
  }
}

class _FakeListAdapter extends ListAdapter<_TestItem> {
  final _items = ListBuilder<_TestItem>();
  int _id = 0;

  _FakeListAdapter(Iterable<_TestItem> items) {
    _items.replace(items);
  }

  @override
  Future<_TestItem> create(_TestItem data) async {
    final created = (data.toBuilder()..id = ++_id).build();
    _items.add(created);
    return created;
  }

  @override
  Future<void> delete(_TestItem item) async {
    _items.where((i) => i.id != item.id);
  }

  @override
  Future<BuiltList<_TestItem>> listAll() async {
    return _items.build();
  }

  @override
  Future<ListPage<_TestItem>> listPaginated({int page, int size}) {
    // TODO: implement listPaginated
    throw UnimplementedError();
  }

  @override
  Future<_TestItem> retrieve(_TestItem item) async {
    return _items
        .build()
        .singleWhere((i) => i.id == item.id, orElse: () => null);
  }

  @override
  Future<_TestItem> update(_TestItem data) async {
    if (!_items.build().any((i) => i.id == data.id)) {
      throw StateError('Item oes not exists.');
    }
    _items.map((i) => i.id == data.id ? data : i);
    return data;
  }
}

abstract class _TestItem implements Built<_TestItem, _TestItemBuilder> {
  int get id;
  String get name;

  _TestItem._();
  factory _TestItem(int id, String name) => _$TestItem((b) => b
    ..id = id
    ..name = name);
}
0
likes
80
pub points
0%
popularity

Extra voilerplates for boc package.

Homepage

Documentation

API reference

Uploader

francipvbelcapo@gmail.com

License

MIT (LICENSE)

Dependencies

bloc, built_collection, built_value, meta

More

Packages that depend on bloc_extras