The error message suggests that you are trying to cast a non-nullable type to a nullable type, which is not allowed. Since genres is an optional list (List<int>?), you need to check whether it is null before casting it to a non-null List<int>.
You can modify your router code as follows:
final GoRouter _router = GoRouter(routes: [
GoRoute(
name: 'loading',
path: '/',
builder: (context, state) {
List<int>? genres = state.extra as List<int>?;
return Loading(genres: genres);
},
),
...
]);
In this code, the genres variable is declared as a nullable list (List<int>?). The cast to List<int>? allows for null values, so if state.extra is null, genres will be set to null as well. If state.extra is not null, it will be cast to a non-null List<int> and assigned to genres.
Then, you can pass genres to the Loading widget, which is also an optional list, so you don't need to do any additional null checks inside the widget.
To know more, join our Flutter Training today.