How to cast Object to a specified type in Flutter

Viewed 23870

I get a linter error but don't know how to fix it

final FoodScreenArguments args = ModalRoute.of(context).settings.arguments;

A value of type Object can't be assigned to a variable of type FoodScreenArguments.

Try changing the type of the variable, or casting the right-hand type to FoodScreenArguments .

2 Answers

Easiest way :

final args = ModalRoute.of(context).settings.arguments as FoodScreenArguments ;

Unlike java's parentheses casting (), in flutter, it uses as keyword. Here is an example from my code where I am printing a variable of class.

debugPrint("rht: List size ${((albumList1.first) as AlbumData).title}");
Related