Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigator.pop(context); shows RangeError (index): Index out of range: index must not be negative: -1 #420

Closed
sivaram16 opened this issue Dec 9, 2021 · 9 comments
Labels
bug Something isn't working
Milestone

Comments

@sivaram16
Copy link

Describe the bug
When using Navigator.pop(context) in the latest version of beamer(1.0.0), It throws the error Navigator.pop(context); shows RangeError (index): Index out of range: index must not be negative: -1. This problem happens when using context.beamTo instead of context.beamToNamed.

Beamer version: (v1.0.0)

To Reproduce
Steps to reproduce the behavior:

  1. In the location builder example of this plugin location_builder_example.
  2. Change context.beamToNamed to context.beamTo.
          context.beamTo(
                 BooksLocation()
                   ..state = BeamState(
                     pathPatternSegments: [
                       'books',
                       ':bookId',
                     ],
                     pathParameters: {"bookId": book['id'].toString()},
                   ),
               ),
  1. Try hitting the back button of the book description screen.
  2. You can find the above-mentioned error.

Expected behavior
To go back to previous screen instead of the error.

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser: Chrome

Additional context
Flutter version: 2.5.3

@slovnicki slovnicki added this to the v1.1 milestone Dec 12, 2021
@slovnicki slovnicki added the bug Something isn't working label Dec 12, 2021
@slovnicki
Copy link
Owner

slovnicki commented Dec 12, 2021

Hey @ThalapathySiva 👋
Thanks for creating an issue and providing clear steps how to reproduce.

I was able to reproduce it and make a fix in e47ddf7.

I would suggest using the super constructor to handle the creation of new state for YourBeamLocation. And in this specific use case, it would be better to update the state on currentBeamLocation instead of creating a new one.

@sivaram16
Copy link
Author

@slovnicki Thanks for your fix and suggestions and when can I expect this fix in the release?

@slovnicki
Copy link
Owner

slovnicki commented Dec 13, 2021

@ThalapathySiva The v1.1.0 release is planned somewhere within next few days, when other linked issues and PRs are resolved. You can inspect the goals and progres for v1.1 milestone here: https://github.com/slovnicki/beamer/milestone/16

Edit: it's published.

@sivaram16
Copy link
Author

sivaram16 commented Dec 14, 2021

@slovnicki Yes I have checked and It worked and I have another doubt to ask,

In the same location builder example, you have book and book details screen in same location(BookLocation). But when you have a separate location for the book details screen as I mentioned below, If you navigate to details screen and you hit the back button pop does not work, but browser back button will work just fine.

class BooksLocation extends BeamLocation<BeamState> {
  BooksLocation({RouteInformation? routeInformation}) : super(routeInformation);

  @override
  List<String> get pathPatterns => [
        '/',
        '/books',
      ];

  @override
  List<BeamPage> buildPages(BuildContext context, BeamState state) {
    return [
      BeamPage(
        key: ValueKey('home'),
        title: 'Home',
        child: HomeScreen(),
      ),
      if (state.uri.pathSegments.contains('books'))
        BeamPage(
          key: ValueKey('books'),
          title: 'Books',
          child: BooksScreen(),
        ),
    ];
  }
}

class BooksDetailLocation extends BeamLocation<BeamState> {
  BooksDetailLocation({RouteInformation? routeInformation})
      : super(routeInformation);

  @override
  List<String> get pathPatterns => [
        '/',
        '/books/:bookId',
      ];

  @override
  List<BeamPage> buildPages(BuildContext context, BeamState state) {
    return [
      BeamPage(
        key: ValueKey('book-${state.pathParameters['bookId']}'),
        title: books.firstWhere(
            (book) => book['id'] == state.pathParameters['bookId'])['title'],
        child: BookDetailsScreen(
          book: books.firstWhere(
              (book) => book['id'] == state.pathParameters['bookId']),
        ),
      ),
    ];
  }
}

And Navigate like below,

context.beamTo(
                 BooksDetailLocation()
                        ..state = BeamState(
                          pathPatternSegments: [
                            'books',
                            ':bookId',
                          ],
                          pathParameters: {"bookId": book['id'].toString()},
                        ),
                     );

And finally it would be great if you show me some example to use the super constructor to handle the creation of new state for beam location as you suggested.

@sivaram16
Copy link
Author

Hi @slovnicki any update on this ?

@slovnicki
Copy link
Owner

Hey @ThalapathySiva, sorry for a late response.

This is expected.
AppBar's back button will do a pop and browser's back button will just navigate to last URL. These behaviors are different.

The list of pages you return in buildPages are the pages that the Navigator will stack on top of each other. If there is a page beneath a certain page, we can pop. In the case of your BookDetailLocation, you have just one page in the navigator stack so it cannot pop.

You can read more about this in the Navigating Back section of README. Browser's back button is "reverse chronological", I should probably mention that in README.

@sivaram16
Copy link
Author

Can you please let me know the correct way of doing beamTo to particular location along with parameters like I have done above. @slovnicki

@slovnicki
Copy link
Owner

@ThalapathySiva It depends on what you want.
I have described in my above comment the difference between various back navigations.

The most natural way would be to have all books related pages in the same BeamLocation and stacked on top of each other, like in the example

Best is to navigate with, for example beamToNamed('/books/2), but beamTo you provided above should works the same.

@sivaram16
Copy link
Author

@slovnicki Thanks for the reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants