Skip to content

Commit

Permalink
Fix Bad state: Cannot emit new states
Browse files Browse the repository at this point in the history
  • Loading branch information
proninyaroslav committed Sep 10, 2024
1 parent d97ff13 commit d56ede7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion flatpak
11 changes: 8 additions & 3 deletions lib/ui/accounts/model/accounts_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'package:bloc/bloc.dart';
import 'package:libretrack/core/storage/service_repository.dart';
import 'package:libretrack/core/storage/storage_result.dart';
import 'package:libretrack/ui/accounts/model/accounts_state.dart';
import 'package:libretrack/ui/model/utils.dart';

class AccountsCubit extends Cubit<AccountsState> {
final ServiceRepository _serviceRepo;
Expand All @@ -28,12 +29,16 @@ class AccountsCubit extends Cubit<AccountsState> {

Future<void> observeServices() async {
emit(const AccountsState.initial());

final stream = _serviceRepo.observeAllServices();
await for (final result in stream) {
result.when(
(serviceList) => emit(AccountsState.loaded(serviceList)),
error: (e) => emit(AccountsState.loadingFailed(error: e)),
final newState = result.when(
(serviceList) => AccountsState.loaded(serviceList),
error: (e) => AccountsState.loadingFailed(error: e),
);

safeEmit(newState);

if (result is StorageResultError) {
break;
}
Expand Down
10 changes: 10 additions & 0 deletions lib/ui/model/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import 'dart:ui' as ui;

import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:libretrack/core/entity/dimensions.dart';
Expand Down Expand Up @@ -218,3 +219,12 @@ extension DoubleExtension on double {
return truncateToDouble() == this ? toStringAsFixed(0) : toString();
}
}

extension CubitExt<T> on Cubit<T> {
void safeEmit(T state) {
if (!isClosed) {
// ignore: invalid_use_of_visible_for_testing_member, invalid_use_of_protected_member
emit(state);
}
}
}
24 changes: 10 additions & 14 deletions lib/ui/parcel_details/model/parcel_details_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:libretrack/core/storage/shipment_repository.dart';
import 'package:libretrack/core/storage/storage_result.dart';
import 'package:libretrack/core/storage/track_number_repository.dart';
import 'package:libretrack/core/storage/tracking_repository.dart';
import 'package:libretrack/ui/model/utils.dart';
import 'package:libretrack/ui/parcel_details/components/parcel_details.dart';
import 'package:libretrack/ui/parcel_details/model/parcel_details_state.dart';

Expand Down Expand Up @@ -98,23 +99,18 @@ class ParcelDetailsCubit extends Cubit<ParcelDetailsState> {
]);

await for (final result in group) {
result.when(
(info) => emit(
ParcelDetailsState.loaded(
trackNumber: trackNumber,
info: info,
),
),
notFound: () => emit(
ParcelDetailsState.notFound(trackNumber: trackNumber),
final newState = result.when(
(info) => ParcelDetailsState.loaded(
trackNumber: trackNumber,
info: info,
),
failed: (error) => emit(
ParcelDetailsState.loadingFailed(
trackNumber: trackNumber,
error: error,
),
notFound: () => ParcelDetailsState.notFound(trackNumber: trackNumber),
failed: (error) => ParcelDetailsState.loadingFailed(
trackNumber: trackNumber,
error: error,
),
);
safeEmit(newState);

if (result is _BuildResultFailed) {
break;
Expand Down
35 changes: 16 additions & 19 deletions lib/ui/parcels/model/parcels_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'package:libretrack/core/storage/shipment_repository.dart';
import 'package:libretrack/core/storage/storage_result.dart';
import 'package:libretrack/core/storage/track_number_repository.dart';
import 'package:libretrack/core/storage/tracking_repository.dart';
import 'package:libretrack/ui/model/utils.dart';
import 'package:libretrack/ui/parcels/model/parcels_state.dart';

import 'filter.dart';
Expand Down Expand Up @@ -80,7 +81,7 @@ class ParcelsCubit extends Cubit<ParcelsState> {
),
]);
await for (final result in group) {
result.when(
final newState = result.when(
(infoList) {
final active = <ParcelInfo>[];
final archive = <ParcelInfo>[];
Expand All @@ -91,28 +92,24 @@ class ParcelsCubit extends Cubit<ParcelsState> {
archive.add(info);
}
}
emit(
ParcelsState.loaded(
active: active,
archive: archive,
filters: state.filters,
search: state.search,
sort: state.sort,
),
);
},
failed: (error) {
emit(
ParcelsState.loadingFailed(
error: error,
filters: state.filters,
search: state.search,
sort: state.sort,
),
return ParcelsState.loaded(
active: active,
archive: archive,
filters: state.filters,
search: state.search,
sort: state.sort,
);
},
failed: (error) => ParcelsState.loadingFailed(
error: error,
filters: state.filters,
search: state.search,
sort: state.sort,
),
);

safeEmit(newState);

if (result is _BuildResultFailed) {
break;
}
Expand Down

0 comments on commit d56ede7

Please sign in to comment.