Closed thanoofayoob closed 3 weeks ago
That is weird as we do almost the same. Could it be that there has been another scope pushed so your popScope pops the wrong scope? Try popScopeTil instead Am 17. Nov. 2024, 09:48 +0100 schrieb thanoofayoob @.***>:
Issue Summary: I am encountering an issue with the GetIt package in my Flutter application related to the lifecycle of a GraphQLClient instance. Specifically, when a user logs out, I attempt to unregister or isolate the GraphQLClient to avoid carrying over the previous user's data. However, even after introducing scoped dependencies and unregistering the old client, the new user still interacts with the previous user's GraphQL client instance or its remnants, leading to unexpected behavior such as accessing stale data or using outdated headers. Steps to Reproduce: A user logs in, and the AppGraphQLClient is registered using: injector.registerSingleton(() => AppGraphQLClient()); The client includes headers with the user's token for authentication. On user logout, I attempted two approaches: Directly unregistering the GraphQLClient instance: injector.unregister(); Introducing scoped dependencies using: injector.pushNewScope( scopeName: "NewUser", init: (getIt) { injector.registerSingleton(() => AppGraphQLClient()); }, ); The scope is dropped on logout using: injector.popScope(); A new user logs in, triggering a re-registration of the AppGraphQLClient for the new session. Despite these efforts, the new user still interacts with the old client's instance or configuration, including outdated headers like the previous user's token. Expected Behavior: After a user logs out: The previous AppGraphQLClient instance should be completely destroyed, along with any cached data or retained headers. On new user login, a completely fresh AppGraphQLClient instance should be initialized with the new user's token and other required configurations. Observed Behavior: Even after unregistering the previous client or using scoped dependencies with pushNewScope, the new user still interacts with the previous user's instance or data. This leads to stale headers or incorrect token usage. Additional Notes: I do not reset the entire GetIt container on logout, as I want to preserve other dependencies — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>
Nop . There was only one pop was pushed after successful login from the user and poped when user logout.
I will try with popscopeuntil.?
in one of the issue you answered like instead of lazy singleton use normal one. when i tried that it was throwing error saying it is AppGraphqlClient is not regiseterd with get it.
Is there something else i can use to solve the issue.?
Bad state: Tried to read a provider that threw during the creation of its value. The exception occurred during the creation of type DashBoardCubit.
══╡ EXCEPTION CAUGHT BY PROVIDER ╞═══════════════════════════════ The following StateError was thrown: Bad state: GetIt: Object/factory with type AppGraphQLClient is not registered inside GetIt. (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance; Did you forget to register it?)
This was the issue when i tried with injector.pushNewScope(scopeName: "NewUser",init: (getIt) { injector.registerSingleton(() => AppGraphQLClient()); },);
tried with popscopuntil still same issue.
That one is easy, registerSingleton expects an instance not a factory method Am 17. Nov. 2024, 10:12 +0100 schrieb thanoofayoob @.***>:
Bad state: Tried to read a provider that threw during the creation of its value. The exception occurred during the creation of type DashBoardCubit. ══╡ EXCEPTION CAUGHT BY PROVIDER ╞═══════════════════════════════ The following StateError was thrown: Bad state: GetIt: Object/factory with type AppGraphQLClient is not registered inside GetIt. (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance; Did you forget to register it?) This was the issue when i tried with injector.pushNewScope(scopeName: "NewUser",init: (getIt) { injector.registerSingleton(() => AppGraphQLClient()); },); — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
What i tried initially was resetting all dependencies using injector.reset() that function was resetting all dependencies . but the issue was MyAuthController bloc was also registered in get it ,using the stream controller in that bloc i was managing the auth state of my APP. when i reset() ,it stopped emitting stream.
so what the best way to handle logout and when a new user logs in how to clear the previous user data.
Can you try to create a stripped down repro project so I can look into it? I'm very sure that that part of get_it works Am 17. Nov. 2024, 10:20 +0100 schrieb thanoofayoob @.***>:
What i tried initially was resetting all dependencies using injector.reset() that function was resetting all dependencies . but the issue was MyAuthController bloc was also registered in get it ,using the stream controller in that bloc i was managing the auth state of my APP. when i reset() ,it stopped emitting stream. so what the best way to handle logout and when a new user logs in how to clear the previous user data. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
do you have a example repo which handle logout . ?
creating a new repo to demo my issue will take more time. and im running out of time . if it is must i will tries to create one.
is this is the right way ,like using the scope to handle user sessions and logout.
Can you add me to your repo? I might find time a bit later to look at it Am 17. Nov. 2024, 10:28 +0100 schrieb thanoofayoob @.***>:
do you have a example repo which handle logout . ? creating a new repo to demo my issue will take more time. and im running out of time . if it is must i will tries to create one. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
ho sorry. its my company repo . i don't have admin privilege to it.
Absolutely it's my prime example Am 17. Nov. 2024, 10:30 +0100 schrieb thanoofayoob @.***>:
is this is the right way ,like using the scope to handle user sessions and logout. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
Inspect your injector variable at a breakpoint after the popScope (make sure to await the Pop call) Check which scopes are still there. Could it be that you have to clear some locally stored token before the next user logs in? Am 17. Nov. 2024, 10:31 +0100 schrieb thanoofayoob @.***>:
ho sorry. its my company repo . i don't have admin privilege to it. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
https://github.com/fluttercommunity/get_it/issues/108
i have tried this answer of yours . you are also mentioning this right.?
Not directly related. But inspect the data inside get_it and check if your singleton is still registered Am 17. Nov. 2024, 10:44 +0100 schrieb thanoofayoob @.***>:
108
i have tried this answer of your . you are also mentioning this right.? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
no i'm not storing token locally. and i haven't killed the app before the next user login. if the next user is using after killing the app session there is no issue.!
and as i have already mentioned it was working when i reset the injector on logout. but by doing that my bloc was unable to listen stream controller since it was disposed.
yeah inspected it shows
AppLogger().debug("${injector.isRegistered
// injector.unregister<AppGraphQLClient>();
// injector.pushNewScope(scopeName: "NewUser");
await injector.popScope();
AppLogger().info("logout");
AppLogger().debug("${injector.isRegistered<AppGraphQLClient>()}");
after logout it is returning false.
and i have another qn whats the issue with injector.reset()? , even after it is initialising the dependencies why i was unable to listen to streamcontroller changes.?
If the isRegistered returns false you keep the state somewhere else. Then it's definitely not related to get_it Am 17. Nov. 2024, 10:54 +0100 schrieb thanoofayoob @.***>:
and i have another qn whats the issue with injector.reset()? , even after it is initialising the dependencies why i was unable to listen to streamcontroller changes.? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
Okay. the thing is that im unable to find any other issues.
i have one more qn is there any workaround with injector.reset().
and after that registering all the dependencies again.?
https://github.com/fluttercommunity/get_it/issues/112
this is almost same issue im facing ,freewebwithme last comment.
If you do the reset and call your startup registration again does it then work? Am 17. Nov. 2024, 11:08 +0100 schrieb thanoofayoob @.***>:
Okay. the thing is that im unable to find any other issues. i have one more qn is there any workaround with injector.reset(). and after that registering all the dependencies again.? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
every other dependencies worked. but as i said my authrepo contain's a streamcontroller which manages authstate of my app. which will be disposed on reset. after that my streamcontroller is not working.
I m absolutely sure you habe some fundamental logic problem in your app. Add more breakpoints, step through inspect your objects using the debugger (I hope you know to do that?) Am 17. Nov. 2024, 11:17 +0100 schrieb thanoofayoob @.***>:
every other dependencies worked. but as i said my authrepo contain's a streamcontroller which manages authstate of my app. which will be disposed on reset. after that my streamcontroller is not working. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
okay. can you please what happens when injector.reset() happens?.
and if you have example repo or github issue to describe manage user logout ,share that also.
and thanks a lot for your time and replies.
reset removes all registrations calling their dispose functions if you registered with a dispose function and removes all scopes. Is it possible that one of your blocs still holds the login state? Make the property that holds your login token to a getter/setter and put breakpoints in it so see what happens. Am 17. Nov. 2024, 11:26 +0100 schrieb thanoofayoob @.***>:
okay. can you please what happens when injector.reset() happens?. and if you have example repo or github issue to describe manage user logout ,share that also. and thanks a lot for your time and replies. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
as i said i have cross checked all the block part it is working fine.
I fear I can't help more. The object is no longer inside get_it after logout. So it's a problem in your code Am 17. Nov. 2024, 11:40 +0100 schrieb thanoofayoob @.***>:
as i said i have cross checked all the block part it is working fine. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
one more doubt resetting all dependencies and initialising it again , is there any problem with that.?
Normally not if a logout should reset your whole apps state. Am 17. Nov. 2024, 11:48 +0100 schrieb thanoofayoob @.***>:
one more doubt resetting all dependencies and initialising it again , is there any problem with that.? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
i solved this issue by implementing interceptors on graphql client. Thanks for the replies and your timw @escamoteur .
Got a lot of ideas how get_it works.
So you found the real problem? Awesome
Are you alre6a member of our new forum? Forum.itsallwidgets.com? Am 18. Nov. 2024, 07:59 +0100 schrieb thanoofayoob @.***>:
i solved this issue by implementing interceptors on graphql client. Thanks for the replies and your timw @escamoteur . Got a lot of ideas how get_it works. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>
flutter graphql client had no documentation about adding interceptors. that why when i get new a token i had to reinitialise the client again , that why i had to reset to unregister the get_it dependency.
yesterday i found a way to add interceptor to my client , So when a new token arrives it will be handled by my interceptors.
class AuthLink extends Link {
@override
Stream
// Clone the request to add the authorization header
final req = request.updateContextEntry<HttpLinkHeaders>(
(headers) => HttpLinkHeaders(
headers: {
...headers?.headers ?? {},
'Authorization': 'Bearer $token' },
),
);
// Forward the modified request
if (forward != null) {
yield* forward(req);
} } }
late GraphQLClient _graphQLClient; final loggingLink = LoggingLink(); final authLink = AuthLink();
// final link = Link.from([errorLink, httpLink, logLiNK, authLink]);
final link =
errorLink.concat(authLink).concat(loggingLink).concat(httpLink);
_graphQLClient = GraphQLClient(
cache: GraphQLCache(),
alwaysRebroadcast: true,
link: link,
queryRequestTimeout: GflConstants.apiTimeout,
defaultPolicies: DefaultPolicies(
query: Policies(fetch: FetchPolicy.networkOnly),
));
Issue Summary: I am encountering an issue with the GetIt package in my Flutter application related to the lifecycle of a GraphQLClient instance. Specifically, when a user logs out, I attempt to unregister or isolate the GraphQLClient to avoid carrying over the previous user's data. However, even after introducing scoped dependencies and unregistering the old client, the new user still interacts with the previous user's GraphQL client instance or its remnants, leading to unexpected behavior such as accessing stale data or using outdated headers.
Steps to Reproduce:
A user logs in, and the AppGraphQLClient is registered using:
injector.registerSingleton(() => AppGraphQLClient()); The client includes headers with the user's token for authentication.
On user logout, I attempted two approaches:
Directly unregistering the GraphQLClient instance:
injector.unregister();
Introducing scoped dependencies using:
injector.pushNewScope( scopeName: "NewUser", init: (getIt) { injector.registerSingleton(() => AppGraphQLClient()); }, ); The scope is dropped on logout using:
injector.popScope(); A new user logs in, triggering a re-registration of the AppGraphQLClient for the new session.
Despite these efforts, the new user still interacts with the old client's instance or configuration, including outdated headers like the previous user's token.
Expected Behavior: After a user logs out:
The previous AppGraphQLClient instance should be completely destroyed, along with any cached data or retained headers. On new user login, a completely fresh AppGraphQLClient instance should be initialized with the new user's token and other required configurations. Observed Behavior: Even after unregistering the previous client or using scoped dependencies with pushNewScope, the new user still interacts with the previous user's instance or data. This leads to stale headers or incorrect token usage.
Additional Notes:
I do not reset the entire GetIt container on logout, as I want to preserve other dependencies