From d56ede7e94de5e3b479999d690bd29e324342d51 Mon Sep 17 00:00:00 2001 From: Yaroslav Pronin Date: Tue, 10 Sep 2024 12:15:51 +0300 Subject: [PATCH] Fix Bad state: Cannot emit new states --- flatpak | 2 +- lib/ui/accounts/model/accounts_cubit.dart | 11 ++++-- lib/ui/model/utils.dart | 10 ++++++ .../model/parcel_details_cubit.dart | 24 ++++++------- lib/ui/parcels/model/parcels_cubit.dart | 35 +++++++++---------- 5 files changed, 45 insertions(+), 37 deletions(-) diff --git a/flatpak b/flatpak index 4170a86..a091294 160000 --- a/flatpak +++ b/flatpak @@ -1 +1 @@ -Subproject commit 4170a8656f97d0e7c4cc218eb47554f8f1f2e56c +Subproject commit a09129420e8f9b176d54e07e7314eb00c4ceb799 diff --git a/lib/ui/accounts/model/accounts_cubit.dart b/lib/ui/accounts/model/accounts_cubit.dart index d0c3b76..028bc6e 100644 --- a/lib/ui/accounts/model/accounts_cubit.dart +++ b/lib/ui/accounts/model/accounts_cubit.dart @@ -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 { final ServiceRepository _serviceRepo; @@ -28,12 +29,16 @@ class AccountsCubit extends Cubit { Future 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; } diff --git a/lib/ui/model/utils.dart b/lib/ui/model/utils.dart index ca0de27..e0cd06e 100644 --- a/lib/ui/model/utils.dart +++ b/lib/ui/model/utils.dart @@ -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'; @@ -218,3 +219,12 @@ extension DoubleExtension on double { return truncateToDouble() == this ? toStringAsFixed(0) : toString(); } } + +extension CubitExt on Cubit { + void safeEmit(T state) { + if (!isClosed) { + // ignore: invalid_use_of_visible_for_testing_member, invalid_use_of_protected_member + emit(state); + } + } +} diff --git a/lib/ui/parcel_details/model/parcel_details_cubit.dart b/lib/ui/parcel_details/model/parcel_details_cubit.dart index c0b0073..ebf5929 100644 --- a/lib/ui/parcel_details/model/parcel_details_cubit.dart +++ b/lib/ui/parcel_details/model/parcel_details_cubit.dart @@ -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'; @@ -98,23 +99,18 @@ class ParcelDetailsCubit extends Cubit { ]); 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; diff --git a/lib/ui/parcels/model/parcels_cubit.dart b/lib/ui/parcels/model/parcels_cubit.dart index 21e8087..de37b46 100644 --- a/lib/ui/parcels/model/parcels_cubit.dart +++ b/lib/ui/parcels/model/parcels_cubit.dart @@ -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'; @@ -80,7 +81,7 @@ class ParcelsCubit extends Cubit { ), ]); await for (final result in group) { - result.when( + final newState = result.when( (infoList) { final active = []; final archive = []; @@ -91,28 +92,24 @@ class ParcelsCubit extends Cubit { 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; }