Flutter passing optional List argument through go router

0 votes

I want to pass an optional List argument through go_router.
Here's my class with an optional List argument:

class Loading extends StatefulWidget {
    List<int>? genres;
    Loading({Key? key, this.genres}) : super(key: key);

    @override
    _LoadingState createState() => _LoadingState();
}

And the router:

final GoRouter _router = GoRouter(routes: [
    GoRoute(name: 'loading', path: '/', builder: (context, state) {
            List<int> genres = state.extra as List;
            return Loading(genres: genres);
        },  
    ), 

  ...
 
]);

But it doesn't work. I get this error message:

How can I fix that ?

Apr 6, 2023 in Flutter by Ashwini
• 5,430 points
2,006 views

1 answer to this question.

0 votes

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.

answered Apr 6, 2023 by Anitha

Related Questions In Flutter

0 votes
1 answer
0 votes
1 answer

How to implement drop down list in flutter?

You're close! The error you're getting is ...READ MORE

answered Mar 26, 2023 in Flutter by vishalini
4,010 views
0 votes
1 answer

How to Deserialize a list of objects from json in flutter Ask Question?

To deserialize a list of objects from ...READ MORE

answered Mar 28, 2023 in Flutter by vishalini
2,222 views
0 votes
1 answer

Linking 3 Flutter Projects in One Flutter Project

Yes, it is possible to combine multiple ...READ MORE

answered Mar 20, 2023 in Flutter by vinayak
663 views
0 votes
1 answer

What is the future of flutter?

Hi@MD, Flutter is a rather new cross-platform framework ...READ MORE

answered Jul 17, 2020 in Others by akhtar
• 38,260 points
1,106 views
0 votes
1 answer

What is Flutter?

Hi@akhtar, Flutter is an app SDK for building ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,460 points
1,398 views
0 votes
1 answer

How to install Flutter in Windows system?

Hi@akhtar, You can follow the below-given steps to ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,460 points
1,751 views
0 votes
1 answer

Is Flutter/Dart valuable in the professional setting?

Yes, Flutter and Dart are valuable skills ...READ MORE

answered Mar 21, 2023 in Flutter by vani
474 views
0 votes
1 answer

How to make flutter app responsive according to different screen size?

To make your Flutter app responsive according ...READ MORE

answered Mar 27, 2023 in Flutter by anonymous
7,228 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP