Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the reason for the error A is not a subtype of B

I recently started learning Clean Architecture from a video tutorial. I tried to repeat after the mentor, but only for my own project. As a result, I ran into an error – Unhandled Exception: type ‘HotSalesEmpty’ is not a subtype of type ‘HotSalesLoading’ in type cast.

Error text – [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled
Exception: type ‘HotSalesEmpty’ is not a subtype of type
‘HotSalesLoading’ in type cast E/flutter (5494): #0
HotSalesCubit.loadHotSales.
(package:architecture/features/presentation/cubit/hot_sales_list_cubit.dart:20:31)
E/flutter (5494): #1 Right.fold (package:dartz/src/either.dart:200:64)
E/flutter (5494): #2
HotSalesCubit.loadHotSales(package:architecture/features/presentation/cubit/hot_sales_list_cubit.dart:19:23)
E/flutter ( 5494):

Here is the code by going to the first link and the third link –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

failureOrHotSales.fold((error) => HotSalesError(message: _mapFailureToMessage(error)), (character) {
      final hotSales = (state as HotSalesLoading).oldHotSalesList;
      hotSales.addAll(character);
      emit(HotSalesLoaded(hotSales));
    });

I think this code will also be useful –

class HotSalesLoading extends HotSalesState {
  final List<HotSalesEntity> oldHotSalesList;

  const HotSalesLoading(this.oldHotSalesList); //Loading characters

  @override
  List<Object> get props => [oldHotSalesList];
}

and HotSalesEmpty –

class HotSalesEmpty extends HotSalesState {
  @override
  List<Object> get props => [];
}

>Solution :

I’m guessing now.. But I suspect that HotSalesEmpty is defined as:

class HotSalesEmpty extends HotSalesState { ...

This means that HotSalesEmpty is not a subtype of HotSalesLoading, but a subtype of HotSalesState.

Before you do this type cast: (state as HotSalesLoading) you could wrap it with a check first:

if (state is HotSalesLoading) {
  final hotSales = (state as HotSalesLoading).oldHotSalesList;
....
} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading