Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch an 'as' typecast exception in flutter

how can i catch an 'as' typecast exception in flutter. For example this causes an expection as the cast wasn't successful.

final success = mapJson['success'] as String;

In Swift we can use a guard let or an if let statement. Is there something similar for flutter/dart?

like image 293
SwiftNewling Avatar asked Oct 31 '25 11:10

SwiftNewling


1 Answers

Extending the Answer of @Christopher you can even catch specific exceptions using the on block and execute exception specific code:

try {

// ...

 } on SomeException catch(e) {

//Handle exception of type SomeException
  print(e)

} catch(e) {

 //Handle all other exceptions
 print(e)

} finally {

  // code that should always execute; irrespective of the exception 
 }
like image 83
Julian2611 Avatar answered Nov 03 '25 01:11

Julian2611



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!