Skip to content

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

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'))
        ],
      ),
    );
  }
}

Activity

  1. changed the title [-][web] app crash after hitting url directly in browser[/-] [+][web] app crashes after hitting url directly in browser[/+] on Feb 12, 2020
  2. yjbanov commented on Feb 13, 2020

    @yjbanov
    Contributor

    I think it's because the framework expects a slash in your route name, e.g. "/product", but in your routes configuration you don't includes slashes. Having said that, perhaps we should handle this better, so I'll schedule this for a review in the near term.

  3. yjbanov commented on Feb 13, 2020

    @yjbanov
    Contributor
  4. added this to the milestone on Feb 13, 2020
  5. imnikhilp commented on Feb 14, 2020

    @imnikhilp
    Author

    @yjbanov with route name "/product", it works.Page reload takes more time to load compared to routing.

  6. mdebbar commented on Feb 14, 2020

    @mdebbar
    Contributor

    @imnikhilp I'm planning to look into this today. There are 2 areas that might be causing this issue:

    1. The web engine prepends "/" to the path in some cases.
    2. The flutter Navigator slices the initial route name using slashes, and I need to check if that logic works or not when there's no leading slash.
  7. thasmin commented on Feb 14, 2020

    @thasmin

    I am getting this error except I do have a slash in front of my route name.

          routes: {
            "/": (context) => HomePage(),
            "/teams": (context) => HomePage(),
          },
    

    This error occurs when the web browser is refreshed when on the /teams page. Adding routes for "teams" and "//teams" doesn't help.

    Could not navigate to initial route.
    The requested route name was: "/teams"
    There was no corresponding route in the app, and therefore the initial route specified will be
    ignored and "/" will be used instead.
    
  8. thasmin commented on Feb 17, 2020

    @thasmin

    @mdebbar Were you able to find the problem? Let me know how I can help.

  9. flutterdev-vinit commented on Mar 31, 2020

    @flutterdev-vinit

    Any updates.

  10. lock commented on Apr 25, 2020

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  11. locked and limited conversation to collaborators on Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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