Skip to content

[web] app crashes after hitting url directly in browser #50639

@imnikhilp

Description

@imnikhilp

Steps to Reproduce

  1. Add two named routes to app using "routes" property of MaterialApp class.
  2. Add two button to each widget.on click of button navigate between screens
  3. Navigation works with button click but if you hit url directly in browser or refresh the page then app crashed.It will not work with bookmarked pages.
**Exception**
The following message was thrown:
Could not navigate to initial route.
The requested route name was: "/product"
There was no corresponding route in the app, and therefore the initial route specified will be
ignored and "/" will be used instead.

** Code **

// my app widget
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo!!!',
      home: Home(),
      routes: {'home': (_) => Home(), 'product': (_) => Product()},
    );
  }
}

// product widget

class Product extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: <Widget>[
          Text('Product'),
          FlatButton(
              onPressed: () {
                Navigator.pushNamed(
                  context,
                  'home',
                );
              },
              child: Text('click'))
        ],
      ),
    );
  }
}

// home widget
class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: <Widget>[
          Text('Home'),
          FlatButton(
              onPressed: () {
                Navigator.pushNamed(
                  context,
                  'product',
                );
              },
              child: Text('click'))
        ],
      ),
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: routesNavigator, Router, and related APIs.platform-webWeb applications specifically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions