Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
The switch case expression type 'Type' must be a subtype of the switch expression type 'BankEvent'

I am getting this error when I try to use switch case in dart. I have an abstract class and two classes extending it. See the code below

abstract class BankEvent{}

class FetchBanks extends BankEvent{}

class DeleteBank extends BankEvent{
  final int bankId;
  DeleteBank(this.bankId);
}

I have to make some implementations inside the handleEvents method depends on the instance of the class I am receiving as parameter. But I am getting the error (The switch case expression type 'Type' must be a subtype of the switch expression type 'BankEvent') in the case statement of switch case. My code for the switch case implementation is below

handleEvents(BankEvent bankEvent){
    switch(bankEvent){
      case FetchBanks:
        break;
    }
  }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Why don't you just change your swith to an if statement, like so:

handleEvents(BankEvent bankEvent){ 
   if(bankEvent is FetchBank){
      // FetchBank stuff
   }else if(bankEvent is DeleteBank){
      // DeleteBank stuff
   }
}

This works and is quite readable. Besides, you can get rid off the switch approach.

over 4 years ago · Santiago Trujillo Report

0

Yeah finally I found the answer after a bit of research.It should be writtern as below

handleEvents(BankEvent bankEvent){
    switch(bankEvent.runtimeType){
      case FetchBanks:
        break;
    }
  }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!