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

Get $id without context #12

Closed
Medformatik opened this issue Aug 14, 2022 · 2 comments
Closed

Get $id without context #12

Medformatik opened this issue Aug 14, 2022 · 2 comments

Comments

@Medformatik
Copy link

Is there a way to get the $id of the current user in initState, since I can't use context there?

Here is an example:

@override
void initState() {
    super.initState();
    boatsStream = DB.boat.streamBoats(uid); // Initialize the stream
}

I somehow need to get the users id here. But it's not only initState functions where I need to get the $id without context, for example in the toMap function of some of my models I also need it.

@Medformatik
Copy link
Author

For the example above I can just move the initialization to the build method, but e.g. in my models there is no way of getting the context.

@lohanidamodar
Copy link
Owner

You need to refactor you code to use it other way. It works with context because we do need context to access the state data. As we are using the inherited widget, we need context to access the data. You can easily refactor your code to pass context.
For initState you could move your logic to didChangeDependencies (https://api.flutter.dev/flutter/widgets/State/didChangeDependencies.html)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants