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
E/flutter (5494): #1 Right.fold (package:dartz/src/either.dart:200:64)
E/flutter (5494): #2
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;

I think this code will also be useful –

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

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

  List<Object> get props => [oldHotSalesList];

and HotSalesEmpty –

class HotSalesEmpty extends HotSalesState {
  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;

Leave a Reply