Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Is there a possibility to use abstract Cubits with generic states?

$
0
0

I got stuck on the problem of using a generic type as a Cubit state. The problem is the immutability of Cubit states in combination with the inability to call copyWith for a generic type. A similar implementation based on ChangeNotifier does not cause problems, since we can mutate the returned state.

For example, I have created a basic Cubit having generic state, which I plan to extend. Then I implementing generic type of cubit state by adding a field and copyWith() to it.

abstract class BasePageCubit<TParams extends BasePageParams>    extends Cubit<TParams> {  BasePageCubit({    required TParams initialState,  }) : super(initialState);  @mustCallSuper  void initialise() {    emit(state);  }  @protected  void markLoaded() {    emit(      **state.copyWith(isLoading: false)**,    );  }}@CopyWith()class DataParams<TData> extends BasePageParams {  TData data;  DataParams({    required this.data,    super.isLoading = true,    super.pageError,  });}

Obviously, I can't use state.copyWith(isLoading: false) in BasePageCubit, but I don't see any other ways to implement expandeble logic working with the Cubit inherited from BasePageCubit. Is this a fundamental limitation of the Dart type system, or are there any workarounds? Maybe I misunderstand Bloc pattern with inheritance?


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>