I want to instantiate a Widget based on an int.
const List<Widget> WIDGETS = <Widget>[
Widget1,
Widget2,
Widget3,
Widget4,
];
final int index = 1;
Navigator.pushReplacement(context, MaterialPageRoute<Widget>(builder: (BuildContext context) => WIDGETS[index](hey: index))));
However, I am getting the following error in the Widget list:
The element type 'Type' can't be assigned to the list type 'Widget'. dart(list_element_type_not_assignable)
I don't want to use dynamic or something similar, is there any way to declare that these are Widget Types?
The JS similar would be typeof.
This is possible:
void a() {
print('a');
}
void b() {
print('b');
}
const List<Function> EXAM_SCREENS = <Function>[
a,
b,
a,
a,
];
Why isn't my scenario as such?
Edit: Just to clarify, I need to pass dynamic arguments to these widgets.
Widget1needs theheyargument which is generated in its own Widget.
Widget1()Navigator.pushReplacementfunction is called.