Navigation with named routes

Asked

Viewed 1,163 times

3

I am trying to create some routes named in flutter. When clicking on a floating button, I should direct myself to a screen called new_flight.Dart however, I am seeing an exception:

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

I’ve already checked the routes, but I can’t find the error. The initial route is working, but when I try to move to another screen, it’s generating this message.

Follow the code below:

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

estrutura do projeto

1 answer

3


Do it this way

Modify this part of your code

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

for

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.