Navigation with named routes

I'm trying to create some named routes in flutter. Clicking a floating button should direct me to a screen called new_flight.dart however, I'm getting an exception:

I/flutter ( 6929): Another exception was thrown: Could not find a generator for route RouteSettings("/new_flight", null) in the _WidgetsAppState.

I have already reviewed the routes, but could not find the error. The initial route is working, but when I try to move to another screen, it is generating this message.

Below is the code:

Main.dart

import 'package:flutter/material.dart';
import 'package:ufly/screens/new_flight.dart';
import 'package:ufly/widgets/drawer.dart';
import 'package:ufly/widgets/bottom_navigation.dart';

// Entry Point
void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    title: "Ufly BizJets",
    home: HomeScreen(),
    routes: {
      '/': (context) => HomeScreen(),
      '/new_flight': (context) => NewFlight(),
    },
  ));
}

class HomeScreen extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        drawer: drawerSidebar(),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Stack(
              children: <Widget>[
                Container(
                  height: 340.0,
                  width: double.infinity,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                          bottomRight: Radius.circular(0.0),
                          bottomLeft: Radius.circular(0.0)),
                      image: DecorationImage(
                          image: AssetImage('assets/img/aviation.jpg'),
                          fit: BoxFit.cover)),
                ),
                AppBar(
                  backgroundColor: Colors.transparent,
                ),
                Container(
                  padding: EdgeInsets.only(top: 100.0, left: 20.0),
                  child: Text(
                    "EXPLORE, FIND, TRAVEL",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 12.0,
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(top: 120.0, left: 20.0),
                  width: double.infinity,
                  child: Text(
                    "Where will be your next flight?",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 32.0,
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ],
            ),
            Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(top: 20.0, left: 20.0),
                  child: Text(
                    "Trendings flights",
                    style: TextStyle(
                        fontSize: 20.0,
                        fontWeight: FontWeight.bold,
                        color: Color.fromRGBO(100, 100, 100, 1.0)),
                  ),
                )
              ],
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: SizedBox(
                    height: 160.0,
                    child: ListView(
                      scrollDirection: Axis.horizontal,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(left: 20.0, top: 10.0),
                          decoration: BoxDecoration(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(5.0)),
                              image: DecorationImage(
                                  image: AssetImage(
                                      'assets/img/destinations/paris.jpg'),
                                  fit: BoxFit.cover)),
                          width: 140.0,
                          height: 150.0,
                        ),
                        Container(
                          margin: EdgeInsets.only(left: 20.0, top: 10.0),
                          decoration: BoxDecoration(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(5.0)),
                              image: DecorationImage(
                                  image: AssetImage(
                                      'assets/img/destinations/london.jpg'),
                                  fit: BoxFit.cover)),
                          width: 140.0,
                          height: 150.0,
                        ),
                        Container(
                          margin: EdgeInsets.only(left: 20.0, top: 10.0),
                          decoration: BoxDecoration(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(5.0)),
                              image: DecorationImage(
                                  image: AssetImage(
                                      'assets/img/destinations/paris.jpg'),
                                  fit: BoxFit.cover)),
                          width: 140.0,
                          height: 150.0,
                        ),
                        Container(
                          margin: EdgeInsets.only(left: 20.0, top: 10.0),
                          decoration: BoxDecoration(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(5.0)),
                              image: DecorationImage(
                                  image: AssetImage(
                                      'assets/img/destinations/paris.jpg'),
                                  fit: BoxFit.cover)),
                          width: 140.0,
                          height: 150.0,
                        ),
                      ],
                    ),
                  ),
                )
              ],
            )
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => {Navigator.pushNamed(context, '/new_flight')},
          backgroundColor: Color.fromRGBO(0, 156, 135, 1.0),
          hoverColor: Color.fromRGBO(1, 133, 115, 1.0),
          tooltip: "New flight",
          child: Icon(Icons.flight),
          elevation: 4.0,
        ),
        bottomNavigationBar: bottomNavigation());
  }
}

New_flight.dart

import 'package:flutter/widgets.dart';

class NewFlight extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Project Structure

project structure

Author: Joao Barbosa, 2019-10-24

1 answers

Do as follows

Modify this part of your code

void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    title: "Ufly BizJets",
    home: HomeScreen(),
    routes: {
      '/': (context) => HomeScreen(),
      '/new_flight': (context) => NewFlight(),
    },
  ));
}

By

void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    title: "Ufly BizJets",
    initialRoute: '/',
    routes: {
      '/': (context) => HomeScreen(),
      '/new_flight': (context) => NewFlight(),
    },
  ));
}

You were using two different forms of navigation, use only the NamedRoutes.

I used this link as a reference: Navigate with named routes

 3
Author: Matheus Ribeiro, 2019-10-24 11:03:29