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

StreamChat is overriding onGenerateRoute when navigating using named route #47

Closed
brizaldi opened this issue May 13, 2020 · 14 comments
Closed
Labels
bug Something isn't working

Comments

@brizaldi
Copy link

Expected results:

When using Navigator.pushNamed it goes to my own onGenerateRoute from my main.dart

Actual results:

instead it goes to onGenerateRoute inside stream_chat.dart:

child: Navigator(
  initialRoute: '/',
  key: _navigatorKey,
  onGenerateRoute: (settings) {
    return MaterialPageRoute(
      settings: settings,
      builder: (_) => widget.child,
    );
  },
),
@imtoori imtoori added the bug Something isn't working label May 13, 2020
@imtoori
Copy link
Contributor

imtoori commented May 13, 2020

We added a navigator to make the StreamChat widget always be on top of the widget tree.
This approach is not the best and we should refactorize it finding a good compromise between usability and convenience.
The point is to make the user write as less code as possible to make a working app.

I'll think about it and hopefully come with a better solution.

For the moment you can try to use
Navigator.of(context, rootNavigator: true).pushNamed('/test'),

@brizaldi
Copy link
Author

now i get this error when i call StreamChat.of(context).client :

You must have a StreamChat widget at the top of your widget tree

@imtoori
Copy link
Contributor

imtoori commented May 14, 2020

Are you trying to use StreamChat.of in your new route?
The problem is that once you push a new route, the StreamChat widget is not in your current widget tree anymore.
You may want to take a look at this https://stackoverflow.com/questions/49968817/flutter-inherited-widget-and-routes

A possible solution is to put the widget StreamChat in the builder method of your MaterialApp (like this answer https://stackoverflow.com/questions/49328362/get-access-to-the-context-of-inheritedwidget/49329255#49329255)
I think that's a better solution and in the next version, we can get rid of the additional Navigator (it was a workaround for this situation) suggesting to put the StreamChat widget there.

@imtoori
Copy link
Contributor

imtoori commented May 14, 2020

Could you try the last version using the StreamChat widget in the builder method of the MaterialApp?

@brizaldi
Copy link
Author

after using the last version and put StreamChat inside the builder, i can confirm that its working perfectly now, thank you for the quick response and maybe you can close this issue

@imtoori
Copy link
Contributor

imtoori commented May 15, 2020

Thank you for reporting the issue :)

@imtoori imtoori closed this as completed May 15, 2020
@pixnbit
Copy link

pixnbit commented Jul 9, 2020

I was confused with that builder usage under MaterialApp and got rid of it, then I saw the same message error. I also use named routes.
It's not a clean solution, having the StreamChat initialized this early and at such high scope. Look forward to the better solution. And in the meantime, maybe you can comment in the sample code that builder: was a temporary hack.

@imtoori
Copy link
Contributor

imtoori commented Jul 9, 2020

I think that's true, but you can always just initialize the client and call client.setUser deeper in the tree.
That was the simplest way to avoid to pass the StreamChat widget on every Navigator.push.

We're trying to find a better solution by the way.

@siddhartha2000v
Copy link

siddhartha2000v commented May 8, 2021

I think that's true, but you can always just initialize the client and call client.setUser deeper in the tree.
That was the simplest way to avoid to pass the StreamChat widget on every Navigator.push.

We're trying to find a better solution by the way.

I think that's true, but you can always just initialize the client and call client.setUser deeper in the tree.
That was the simplest way to avoid to pass the StreamChat widget on every Navigator.push.

We're trying to find a better solution by the way.

@imtoori just wanted to check if this issue was resolved in the latest version.I am working on an application and dont want to initialise the stream client above authentication. I am getting the same error quoted above 'You must have a StreamChat widget at the top of your widget tree'

@imtoori
Copy link
Contributor

imtoori commented May 8, 2021

hey @siddhartha2000v this is not really an issue, but we've found a better solution than using the builder parameter of the MaterialApp and we're working on it

Can you give me more information about your issue?
I think the best way to use the StreamChat widget at the moment is using the builder parameter of the MaterialApp as you can see in our sample apps
The initialization using client.connectUser can even happen deeper in the widget tree, but the StreamChat widget would be better placed in there

@siddhartha2000v
Copy link

Thanks for the update @imtoori. I am able to resolve the issue using the builder parameter in material app. However was looking for the update on better solution. Thanks for helping with current status anyways.

@barancezayirli
Copy link

it is a really bad design choice I think. The chat itself would require authentication (need to get a token for a specific user min). And as far as I see, no plan to change it :/

I am building an app, not concentrated on chat but have chat with as an extra feature. Users should log in (this is where they will get the token with their id). When a first-time user opens the app, there is no user so no token.

Furthermore, users can log out and log in with different credentials. That time the client will be initialized with a different token.

Any suggestions on what can I do without putting StreamChat into the builder?

@luvishq
Copy link

luvishq commented Jul 9, 2022

@barancezayirli : were you able to solve the issue ? I am in a similar situation right now . @imtoori : any solution for this ?

@imtoori
Copy link
Contributor

imtoori commented Jul 11, 2022

@luvishq what's your issue?
Putting the StreamChat into the builder is always required, but you could call connectUser even deeper in your tree.
Let me know your issue

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

6 participants