Home > Enterprise >  In Flutter, use key value for widget rather than indexed array
In Flutter, use key value for widget rather than indexed array

Time:04-27

I am creating a list/array of widgets that will be used for routes in flutter as below

final routeScreens = [
  const Login(),
  const LandingPage(), 
];

MaterialApp(
  routes: {
    RoutesPath.loginPath: (context) => routeScreens[0],
    Routes.landingPage: (context) => routeScreens[1],
  },
);

How do I change this from list to an object? As in I would like to access routeScreen['login'] rather than using the index.

I tried the below code, but it gives return type is not widget

final routeScreens = {
  'login' : const Login(),
  'landing' : const LandingPage(), 
};

CodePudding user response:

Have you tried specifying a generic type because it's otherwise Map<dynamic,dynamic>

final routeScreens = <String, Widget>{
  'login' : const Login(),
  'landing' : const LandingPage(), 
};
  • Related