Skip to content
New issue

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

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

Already on GitHub? # to your account

[Stacked Services] currentRoute is always empty #218

Closed
bdairy opened this issue Feb 14, 2021 · 16 comments
Closed

[Stacked Services] currentRoute is always empty #218

bdairy opened this issue Feb 14, 2021 · 16 comments
Labels
new feature Requires planning and detailed comments of what will be done to build this

Comments

@bdairy
Copy link

bdairy commented Feb 14, 2021

Hello there,

I am using the navigation service in my app..

I have a view (QR scanner view) that is accessable from 2 places..

the first place is as a startup view,,, in this case I am showing a button to take me to the dashboard..

the second place is in the dashboard... in this case I am not showing a button to take me to the dashboard, I am just using the back button.

when I access the view from the dashboard simply by calling
locator<NavigationService>().navigateTo(QRScannerRoute);

In the QRScannerView I want to check if my currentRoute is empty "which means It is the startup view" Show the link to take me to the dashboard... but if my current route is not empty which indicates I accessed it from the dashboard just use the default backButton... But in all the cases currentRoute is always empty,,, same applies to the previous route..

To summarise...

locator<NavigationService>().navigateTo(QRScannerRoute);

leads to empty currentRoute in the target view always.

also I couldn't find a way to access the navigation stack to check if it is empty or not..

@bdairy bdairy changed the title currentRoute is always empty [Stacked Services] currentRoute is always empty Feb 18, 2021
@FilledStacks
Copy link
Contributor

@bdairy we use get's current route functionality directly so we don't have any code around how that works in the package. We index into Get.currentRoute . Can you put your main.dart file in here. I'd like to check for setup errors.

@bdairy
Copy link
Author

bdairy commented Feb 19, 2021

@FilledStacks I admire how you care about your product, you deserve all the support..

Main.dart file is quite big and chunked into many files,, I am using a scoped model wrapper for the whole app for localization purposes, and theme is separated..
The nav service is working except the mentioned issue.
I'll try to put what might be relevance to the navigation, I hope it helps:

main.dart

void main() {
  setupLocator();
  setupDialogUi();
  setupBottomSheets();

  runApp(new ScopeModelWrapper());
}

My App

class MyApp extends StatelessWidget {
  // This widget is the root of your application. Main App separated with scoped model descendent
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: Firebase.initializeApp(),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Center(child: Text('Invalid App'));
          }

          // Once complete, show your application
          if (snapshot.connectionState == ConnectionState.done) {
            FlutterError.onError =
                FirebaseCrashlytics.instance.recordFlutterError;
            if (kDebugMode) {
              // Force disable Crashlytics collection while doing every day development.
              // Temporarily toggle this to true if you want to test crash reporting in your app.
              FirebaseCrashlytics.instance
                  .setCrashlyticsCollectionEnabled(true);
            } else {
              // Handle Crashlytics enabled status when not in Debug,
              // e.g. allow your users to opt-in to crash reporting.
            }
            return ScopedModelDescendant<AppModel>(
                builder: (context, child, model) {
              return FutureBuilder(
                  future: model.fetchLocale(),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Center(child: CircularProgressIndicator());
                    }
                
                    return MaterialApp(
                      localizationsDelegates: [
                        AppLocalizationsDelegate(),
                        GlobalMaterialLocalizations.delegate,
                        GlobalWidgetsLocalizations.delegate
                      ],
                      navigatorObservers: [
                        locator<AnalyticsService>().getAnalyticsObserver(),
                      ],
                      supportedLocales: [Locale('en', ''), Locale('ar', '')],
                      locale: snapshot.data as Locale,
                      title: 'Flutter Demo',
                      theme: SystemTheme.mainTheme(
                          (snapshot.data as Locale).languageCode),
                      initialRoute: router.Routes.startupScreen,
                      navigatorKey: locator<NavigationService>().navigatorKey,
                      onGenerateRoute: router.Router().onGenerateRoute,
                    );
                  });
            });
          }

          return Center(child: CircularProgressIndicator());
        });
  }
}

@FilledStacks
Copy link
Contributor

Everything looks perfect. The only "outdated" code is the navigator key. There's a static property in the latest stacked_services now which you can use.

navigatorKey: StackedService.navigatorKey,

That shouldn't fix the problem though. We're using gets built in functionality, maybe the way we're using the routing functionality is causing the issue. I'll need to check into the get package to see when they update the currentRoute to be able to provide ay feedback.

I will schedule some time later today to look at that and provide you with feedback.

@bdairy
Copy link
Author

bdairy commented Feb 25, 2021

Thanks for the Update hint, I am now using the StackedService.navigatorKey..

First of all

Printing the current route like this will give me the route name:

print(ModalRoute.of(context).settings.name);

A simple nav.navigateTo(someRoute)... on the target route loads I am checking the currentRoute.. and it is empty... _navigationService.currentRoute.isEmpty always returns true.

One note,,, this statement is inside a function in the builder method of the ViewModel...

but the current route in Stacked Service is still empty

Sooo check this out:

return ViewModelBuilder<QRScanViewModel>.reactive(
    builder: (context, model, child) => Scaffold(
      appBar: MainAppBarView(
        context: context,
        showPoints: false,
        actions: 
          model.getAction() // This Method
        ,
      ),
      body: Container()
,
,
,

model.getAction method is this:

 List<Widget> getAction() {
    if (_navigationService.currentRoute.isEmpty) {
 // This is always True even if the current route is coming from nav.navigateTo(someRoute).
      return [
        IconButton(
          icon: Icon(Icons.home),
          onPressed: () {
/ / Do Something
          },
        )
      ];
    } else {
      return null;
    }
  }

@FilledStacks
Copy link
Contributor

@bdairy that's most likely because the route has not been set by the time that function executes. You're asking for the current route before it's been built and set. Can you try and delay that call and see if current route is set then?

@bdairy
Copy link
Author

bdairy commented Mar 3, 2021

@FilledStacks tried that in all possible ways, in different views and in different locations.. nothing seems to be working,, on the other hand ModalRoute.of(context).settings.name is working even in build methods...

one more thing,, I really admire your responses even though I know your busy with your tutorials.. Thanks..

@FilledStacks
Copy link
Contributor

@bdairy that's strange. Rather use the one that works then. I don't know why it's not working and I don't have any time to allocate to looking at that at the moment.

Thanks for taking the time to update me about it. I appreciate it :)

@bdairy
Copy link
Author

bdairy commented Mar 4, 2021

@FilledStacks thanks I already used the working one,,, I'll let you know if anything came across..

@AndyMeagher
Copy link

Also experiencing this issue

@akifarhan
Copy link
Contributor

akifarhan commented Mar 18, 2021

Do you guys have any workaround for this issue? I'm trying to get the current route in some services without context. It seems like not possible with ModalRoute

@FilledStacks
Copy link
Contributor

@akifarhan we're actually also experiencing this at the moment. We've had other issues with the get context as well. We will make time to look into our own abstraction for the context-less navigation and tackle all of this as well. I don't know when that'll be. we need the #195 null migration issue to be ready before making big moves.

@FilledStacks FilledStacks added the new feature Requires planning and detailed comments of what will be done to build this label Mar 21, 2021
@DirtyNative
Copy link

I am also facing this issue, ModalRoute does not work for me, so I need to write a wrapper for this

@bbedward
Copy link
Contributor

We also ran into this, ModalRoute doesn't work for us because it's in a nested route.

@FilledStacks
Copy link
Contributor

I have no idea why the functionality isn't working as expected. I'll probably have to track this manually in our navigation service and build an internal stack to keep in sync.

FilledStacks added a commit that referenced this issue Apr 12, 2021
@FilledStacks
Copy link
Contributor

Finally got around to fixing this. Here's the documentation for it. Thanks @hurbes

@bbedward
Copy link
Contributor

This still doesn't seem to work with nested routes, e.g.

  MaterialRoute(page: LoginView, initial: true),
  MaterialRoute(
    page: DrawerNavigatorView,
    children: [
      CustomRoute(page: HomeView, initial: true),
      CustomRoute(page: ReportsView),
      CustomRoute(page: TeamView),
      CustomRoute(page: SettingsView),
      CustomRoute(page: SupportView),
    ],
  ),

Using currentRoute when on one of the nested routes just always returns /

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
new feature Requires planning and detailed comments of what will be done to build this
Projects
None yet
Development

No branches or pull requests

6 participants