Home > Software engineering >  What is the reason for the error A is not a subtype of B
What is the reason for the error A is not a subtype of B

Time:09-08

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 -

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 => [];
}

CodePudding user response:

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;
....
} 
  • Related