Skip to content

Instantly share code, notes, and snippets.

@NicolasCaous
Created December 5, 2019 03:35
Show Gist options
  • Save NicolasCaous/3ce0f4d42cc6966903fe1963c4793552 to your computer and use it in GitHub Desktop.
Save NicolasCaous/3ce0f4d42cc6966903fe1963c4793552 to your computer and use it in GitHub Desktop.
Flutter complex BloC example
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:bloc/bloc.dart';
import 'package:boinalinha/models/farm.dart';
import 'package:boinalinha/services/data_providers/farm_list.dart';
import './bloc.dart';
class FarmListBloc extends Bloc<FarmListEvent, FarmListState> {
final FarmListRepository farmListRepository;
FarmListBloc({@required this.farmListRepository});
@override
FarmListState get initialState => FarmListLoading();
@override
Stream<FarmListState> mapEventToState(
FarmListEvent event,
) async* {
switch (event.runtimeType) {
case LoadFarmList:
yield* _mapLoadFarmListToState();
break;
case LoadFarmListComplete:
yield* _mapLoadFarmListCompleteToState(event);
break;
case LoadFarmListError:
yield* _mapLoadFarmListErrorToState();
break;
case AddFarm:
yield* _mapAddFarmToState(event);
break;
case UpdateFarm:
yield* _mapUpdateFarmToState(event);
break;
case DeleteFarm:
yield* _mapDeleteFarmToState(event);
break;
case SelectFarm:
yield* _mapSelectFarmToState(event);
break;
}
}
Stream<FarmListState> _mapLoadFarmListToState() async* {
this.farmListRepository.loadFarmList().then((List<Farm> farmList) {
this.add(LoadFarmListComplete(farmList));
}).catchError((error) {
print(error);
this.add(LoadFarmListError());
});
}
Stream<FarmListState> _mapLoadFarmListCompleteToState(
LoadFarmListComplete event) async* {
if (state is FarmListLoading) {
yield FarmListLoaded(
event.farmList,
(state as FarmListLoading).selectedFarmIndex,
(state as FarmListLoading).selectedLatLng);
} else if (state is FarmListLoaded) {
yield FarmListLoaded(
event.farmList,
(state as FarmListLoaded).selectedFarmIndex,
(state as FarmListLoaded).selectedLatLng);
}
}
Stream<FarmListState> _mapLoadFarmListErrorToState() async* {
yield FarmListNotLoaded();
}
Stream<FarmListState> _mapAddFarmToState(AddFarm event) async* {
if (state is FarmListLoaded) {
final List<Farm> updatedFarmList =
List.from((state as FarmListLoaded).farmList)..add(event.farm);
yield FarmListLoaded(
updatedFarmList,
(state as FarmListLoaded).selectedFarmIndex,
(state as FarmListLoaded).selectedLatLng);
_saveFarmList(updatedFarmList);
}
}
Stream<FarmListState> _mapUpdateFarmToState(UpdateFarm event) async* {
if (state is FarmListLoaded) {
final List<Farm> updatedFarmList =
(state as FarmListLoaded).farmList.map((farm) {
return farm.address == event.updatedFarm.address
? event.updatedFarm
: farm;
}).toList();
yield FarmListLoaded(
updatedFarmList,
(state as FarmListLoaded).selectedFarmIndex,
(state as FarmListLoaded).selectedLatLng);
_saveFarmList(updatedFarmList);
}
}
Stream<FarmListState> _mapDeleteFarmToState(DeleteFarm event) async* {
if (state is FarmListLoaded) {
final updatedFarmList = (state as FarmListLoaded)
.farmList
.where((farm) => farm.id != event.farm.id)
.toList();
yield FarmListLoaded(updatedFarmList, event.selectedIndex,
(state as FarmListLoaded).selectedLatLng);
_saveFarmList(updatedFarmList);
}
}
Future _saveFarmList(List<Farm> farmList) {
return farmListRepository.saveFarmList(
farmList,
);
}
Stream<FarmListState> _mapSelectFarmToState(SelectFarm event) async* {
if (state is FarmListLoaded) {
yield FarmListLoaded(
(state as FarmListLoaded).farmList,
event.selectedIndex,
event.selectedLatLng != null
? event.selectedLatLng
: (state as FarmListLoaded).selectedLatLng);
} else if (state is FarmListLoading) {
yield FarmListLoading(
event.selectedIndex,
event.selectedLatLng != null
? event.selectedLatLng
: (state as FarmListLoading).selectedLatLng);
}
}
}
import 'package:boinalinha/models/farm.dart';
import 'package:equatable/equatable.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:meta/meta.dart';
@immutable
abstract class FarmListEvent extends Equatable {
const FarmListEvent();
@override
List<Object> get props => [];
}
class LoadFarmList extends FarmListEvent {}
class LoadFarmListComplete extends FarmListEvent {
final List<Farm> farmList;
const LoadFarmListComplete(this.farmList);
@override
List<Object> get props => [farmList];
@override
String toString() => 'Event: LoadFarmListComplete { farmList: $farmList }';
}
class LoadFarmListError extends FarmListEvent {}
class AddFarm extends FarmListEvent {
final Farm farm;
const AddFarm(this.farm);
@override
List<Object> get props => [farm];
@override
String toString() => 'Event: AddFarm { farm: $farm }';
}
class UpdateFarm extends FarmListEvent {
final Farm updatedFarm;
const UpdateFarm(this.updatedFarm);
@override
List<Object> get props => [updatedFarm];
@override
String toString() => 'Event: UpdateFarm { farm: $updatedFarm }';
}
class DeleteFarm extends FarmListEvent {
final Farm farm;
final int selectedIndex;
const DeleteFarm(this.farm, this.selectedIndex);
@override
List<Object> get props => [farm, selectedIndex];
@override
String toString() =>
'Event: DeleteFarm { farm: $farm, selectedIndex: $selectedIndex }';
}
class SelectFarm extends FarmListEvent {
final int selectedIndex;
final LatLng selectedLatLng;
const SelectFarm([this.selectedIndex, this.selectedLatLng]);
@override
List<Object> get props => [selectedIndex, selectedLatLng];
@override
String toString() =>
'Event: SelectFarm { selectedIndex: $selectedIndex, selectedLatLng: $selectedLatLng }';
}
import 'package:boinalinha/models/farm.dart';
import 'package:equatable/equatable.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:meta/meta.dart';
@immutable
abstract class FarmListState extends Equatable {
const FarmListState();
@override
List<Object> get props => [];
}
class FarmListLoading extends FarmListState {
final int selectedFarmIndex;
final LatLng selectedLatLng;
const FarmListLoading([this.selectedFarmIndex = -2, this.selectedLatLng]);
@override
List<Object> get props => [selectedFarmIndex, selectedLatLng];
@override
String toString() =>
'State: FarmListLoading { selectedFarmIndex: $selectedFarmIndex, selectedLatLng: $selectedLatLng }';
}
class FarmListLoaded extends FarmListState {
final List<Farm> farmList;
final int selectedFarmIndex;
final LatLng selectedLatLng;
const FarmListLoaded(
[this.farmList = const [],
this.selectedFarmIndex = -2,
this.selectedLatLng]);
@override
List<Object> get props => [farmList, selectedFarmIndex, selectedLatLng];
@override
String toString() =>
'State: FarmListLoaded { farmList: $farmList, selectedFarmIndex: $selectedFarmIndex, selectedLatLng: $selectedLatLng }';
}
class FarmListNotLoaded extends FarmListState {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment