Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast Object to a specified type in Flutter

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 .

like image 757
zoom xu Avatar asked Feb 09 '21 15:02

zoom xu


People also ask

How to cast a variable to a foodscreenarguments in flutter?

Try changing the type of the variable, or casting the right-hand type to FoodScreenArguments . Unlike java's parentheses casting (), in flutter, it uses as keyword.

Is it possible to use parentheses casting in flutter?

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.

What is cast<T> in C++?

One of the other casting keywords, which lives particularly on Iterables, is cast<T>. It gives you a new list which appears to be of the type List<T>, so that you can use it in places where the analyzer expects List<T>, but it doesn't actually change the types of the elements.

Can you cast from a supertype to a subtype?

Most languages just use the sub- to a supertype. Dart's assignment compatibility rules also allow assigning from a super- to a subtype. needing any kind of explicit cast. So there's no *static* warning here.


1 Answers

Easiest way :

final args = ModalRoute.of(context).settings.arguments as FoodScreenArguments ;
like image 59
Kabirou Agouda Avatar answered Nov 09 '22 22:11

Kabirou Agouda