I am trying to navigate from welcomepage to screen tab but unable to do so because of the above error.
Part of welcome screen.dart
class WelcomePage extends StatelessWidget {
static const routeName = '/welcome-screen';
const WelcomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.black,
child: Stack(
children: [
Positioned.fill(
child: Opacity(
opacity: 0.3,
child: Image.asset('assets/images/of_main_bg.png', fit: BoxFit.cover),
),
),
ThemeButton(
label: "Get Started!",
labelColor: AppColors.MAIN_COLOR,
color: Colors.transparent,
highlight: AppColors.MAIN_COLOR.withOpacity(0.5),
borderColor: AppColors.MAIN_COLOR,
borderWidth: 4,
onPressed: () {Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return TabsScreen(**ERROR**#What should I write here);
},
),
Routes in main.dart
initialRoute: '/', // default is '/'
routes: {
'/': (ctx) => WelcomePage(),
CategoryMealsScreen.routeName: (ctx) => CategoryMealsScreen(_availableMeals),
MealDetailScreen.routeName: (ctx) => MealDetailScreen(_toggleFavorite, _isMealFavorite),
FiltersScreen.routeName: (ctx) => FiltersScreen(_filters, _setFilters),
TabsScreen.routeName: (ctx) => TabsScreen(_favoriteMeals),
WelcomePage.routeName: (ctx) => WelcomePage(),
},
class code in tabscreen.dart
class TabsScreen extends StatefulWidget {
static const routeName = '/tabs-screen';
final List<Meal> favoriteMeals;
TabsScreen(this.favoriteMeals);
How should i navigate from welcomepage to tabs_screen page?In order to navigate for the same what should I write in that ERROR place in welcome_screen.dart. If there's another method for navigation please do tell.
CodePudding user response:
Don't use named routes.
From https://docs.flutter.dev/cookbook/navigation/named-routes
Note: Named routes are no longer recommended for most applications. For more information, see Limitations in the navigation overview page.
CodePudding user response:
Your TabsScreen
constructor is taking positional argument. You can pass empty list([]
) there. But making name constructor with nullable data seems better to me.
class TabsScreen extends StatefulWidget {
static const routeName = '/tabs-screen';
final List<Meal>? favoriteMeals;
TabsScreen({ this.favoriteMeals});
Now it is optional, and you can pass list like TabsScreen(favoriteMeals: yourData)
and skip when you don't want to pass TabsScreen()
.
More about using-constructors