Milad-Akarie / auto_route_library

Flutter route generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pages can't be accessed by path on Web + problem with nested routes

BGGBGIBS opened this issue · comments

commented

Hello my Flutter Web App works with that structure but pages can't be accesed directly from URL and pathparams, could you help me to fix this ?

Also, I can't put autoroutes into the children of an existing autoroute, do you know why ?

@MaterialAutoRouter(
  replaceInRouteName: 'Page,Route',
  routes: <AutoRoute>[
    AutoRoute(
      path: 'WebMenu',
      page: HomePage,
      initial: true,
    ),
    AutoRoute(
      path: 'WebMenu/LogIn',
      page: LoginPage,
    ),
    AutoRoute(
      path: 'WebMenu/SignUp',
      page: SignupPage,
    ),
    AutoRoute(
      path: 'WebMenu/Establishments',
      page: EstablishmentsPage,
    ),
    AutoRoute(
      path: 'WebMenu/:name/Tables',
      page: TablesPage,
    ),
    AutoRoute(
      path: 'WebMenu/:name/:value',
      page: WebMenuPage,
      children: [
        AutoRoute(
            path: 'Drinks',
            name: 'DrinksRouter',
            page: EmptyRouterPage,
            children: [
              AutoRoute(
                path: '',
                page: DrinksPage,
              ),
              AutoRoute(
                path: 'Details/:name',
                page: AddsPage,
              )
            ]),
        AutoRoute(
            path: 'Starters',
            name: 'StartersRouter',
            page: EmptyRouterPage,
            children: [
              AutoRoute(
                path: '',
                page: StartersPage,
              ),
              AutoRoute(
                path: 'Details/:name',
                page: AddsPage,
              ),
            ]),
        AutoRoute(
            path: 'Meals',
            name: 'MealsRouter',
            page: EmptyRouterPage,
            children: [
              AutoRoute(
                path: '',
                page: MealsPage,
              ),
              AutoRoute(
                path: 'Details/:name',
                page: AddsPage,
              )
            ]),
        AutoRoute(
            path: 'Desserts',
            name: 'DessertsRouter',
            page: EmptyRouterPage,
            children: [
              AutoRoute(
                path: '',
                page: DessertsPage,
              ),
              AutoRoute(
                path: 'Details/:name',
                page: AddsPage,
              )
            ]),
        AutoRoute(
          path: 'Basket/:id',
          page: BasketPage,
        ),
      ],
    ),
    AutoRoute(
      path: 'Basket/:id',
      page: LocalBasketPage,
    ),
    AutoRoute(
      path: 'Payment/:id',
      page: PaymentPage,
    ),
  ],
)
class $AppRouter {}

And this is the repository : https://github.com/BGGBGIBS/clean_architecture

Thank you so much !

@BGGBGIBS Can you please explain the issue further?

commented

For the url navigation : when I reload the page from chrome navigator, the page doesn't reload and I get an error "route args can't be null"
I can't parse them from the URL.
For the nested navigation : I get the following error message "seem you are trying to access ********** without adding their parent to stack first" and I get this message each time I try to use nested navigation outside the autotab ..

@BGGBGIBS if you want to avoid this kind of errors for web you should not pass object args only path/query params so that when the page is reloaded you got all the info to build the page.
e.g instead of passing Book pass bookId of type String and let it be a path-param
when you hit reload your whole flutter App looses state and a new instance is initiated with the current url as a starting location.

commented

ok , could you give me a sample code for that ? because I have tried a lot of different codes but no one worked

also, how could I make the app keep the state after reload ?
When I try to access a page from the url, does it also reload or not ?

@BGGBGIBS as far as I know there's no way to keep the state after a reload you can try looking up flutter State restoration thu.
just changing the URL will not reload your App

in my case it reload my App if i change url address

commented

@kencana16 In mine too

@Milad-Akarie could you give us a sample code to show how to use path params to access pages directly from the url ?
I mean, how to parse the path parameters and use it to display data in the app pages ?

Thank you so much

@BGGBGIBS

When you declare your widget that's supposed to be a route, you can decorate the class properties with @PathParam annotation. For example,

// AutoRoute(page: SomeScreenPage, path: '/users/:userID'),
class SomeScreenPage extends StatelessWidget {
  SomeScreenPage({required @PathParam('userID') this.userID});
  // OR because the path param :userID and the class property userID is the same, you can shorten it to:
  SomeScreenPage({required @pathParam this.userID});


  final String userID;
  ...
}

Now, you'll be able to access the URL path param as a variable of your Widget

commented

@Milad-Akarie @NachiketaVadera I still get the following issue :

  • I 'm trying to parse the uri using pathsegments but the uri is always "late" I mean when my url is WebMenu/Pizza, the uri segments are Webmenu, and when the url is WebMenu/Pizza/Cheese, the uri segments are Webmenu and Pizza, how could I fix it ??? thanks

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions