1

I want to redirect the user to the desired page depending on the "navigation" key, I tried in such ways:

final _bottomAppBarItem = [
{
  'iconData': Icons.airplay,
  'text': 'news',
  'navigation': NewsPage(),
},
{
  'iconData': Icons.person,
  'text': 'profile',
  'navigation': UserProfilePage(),
}];

Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => item['navigation']));

OR

final _bottomAppBarItem = [
{
  'iconData': Icons.airplay,
  'text': 'news',
  'navigation': 'NewsPage',
},
{
  'iconData': Icons.person,
  'text': 'profile',
  'navigation': 'UserProfilePage',
}];

Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => item['navigation']()));

But in both cases it turns out a mistake, how can I live with it?

1 Answers1

0

You can use named routing as decribed in here. You can parameterize your route with custom strings.

A simple use case of named route:

onPressed: () {
  // Navigate to the second screen using a named route.
  Navigator.pushNamed(context, '/second');
}

Another solution, which is overhead for this situation, is to use mirrors.

easeccy
  • 4,248
  • 1
  • 21
  • 37