LCOV - code coverage report
Current view: top level - details - details_bloc.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 13 14 92.9 %
Date: 2019-10-10 15:54:03 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:bloc/bloc.dart';
       2             : import 'package:flutter_bloc/flutter_bloc.dart';
       3             : import 'package:flutter_bloc_patterns/details.dart';
       4             : import 'package:flutter_bloc_patterns/src/common/view_state.dart';
       5             : import 'package:flutter_bloc_patterns/src/details/details_events.dart';
       6             : import 'package:flutter_bloc_patterns/src/details/details_repository.dart';
       7             : 
       8             : /// A BLoC that allows to fetch a single element with given identifier.
       9             : ///
      10             : /// Designed to collaborate with [BlocBuilder] and [ViewStateBuilder] for
      11             : /// displaying data.
      12             : ///
      13             : /// Call [loadElement] to fetch an element with given identifier.
      14             : ///
      15             : /// [T] - the type of the element.
      16             : /// [I] - the type of id.
      17             : class DetailsBloc<T, I> extends Bloc<DetailsEvent, ViewState> {
      18             :   final DetailsRepository<T, I> _detailsRepository;
      19             : 
      20           1 :   DetailsBloc(DetailsRepository<T, I> detailsRepository)
      21           0 :       : assert(detailsRepository != null),
      22             :         this._detailsRepository = detailsRepository;
      23             : 
      24           1 :   @override
      25           1 :   ViewState get initialState => Loading();
      26             : 
      27             :   /// Loads an element with given [id].
      28           3 :   void loadElement(I id) => dispatch(LoadDetails(id));
      29             : 
      30             :   @override
      31           1 :   Stream<ViewState> mapEventToState(DetailsEvent event) async* {
      32           1 :     if (event is LoadDetails) {
      33           3 :       yield* _mapLoadDetails(event.id);
      34             :     }
      35             :   }
      36             : 
      37           1 :   Stream<ViewState> _mapLoadDetails(I id) async* {
      38             :     try {
      39           3 :       final element = await _detailsRepository.getById(id);
      40           3 :       yield element != null ? Success<T>(element) : Empty();
      41           1 :     } on ElementNotFoundException {
      42           2 :       yield Empty();
      43             :     } catch (e) {
      44           2 :       yield Failure(e);
      45             :     }
      46             :   }
      47             : }

Generated by: LCOV version 1.14