Hello,Is there any callback function that run on s...
# ask-a-descoper
u
Hello,Is there any callback function that run on session update token like listener in flutter, it would be great help
r
Hi @Uday Singh You mean one callback function which will hit in flutter sdk when flutter app’s session token will update ?
u
yes
r
Hey @Uday Singh Steps to create callback function when session token will update 1. Create a session notifier
Copy code
import 'package:descope/descope.dart';
import 'package:flutter/foundation.dart';

class SessionNotifier extends ChangeNotifier {
  DescopeSession? _session;

  DescopeSession? get session => _session;

  void setSession(DescopeSession? newSession) {
    final oldToken = _session?.sessionJwt;
    final newToken = newSession?.sessionJwt;

    _session = newSession;

    // 🔑 Only fire when token actually changes
    if (oldToken != newToken) {
      notifyListeners();
    }
  }
}
Make this a singleton or provide it via Provider/Riverpod/whatever DI you use. 2. Use it whenever you log in / refresh After Descope login / verify:
Copy code
final authResponse = await Descope.otp.verify(
  method: DeliveryMethod.email,
  loginId: 'user@example.com',
  code: code,
);

final session = DescopeSession.fromAuthenticationResponse(authResponse);
Descope.sessionManager.manageSession(session);

// Update your notifier (this will trigger callback if token changed)
sessionNotifier.setSession(Descope.sessionManager.session);
When refreshing session:
Copy code
await Descope.sessionManager.refreshSessionIfNeeded();
sessionNotifier.setSession(Descope.sessionManager.session);
Rule: never change session directly without passing through sessionNotifier.setSession. 3. Add your “callback” listener Wherever you want the callback (similar to useEffect):
Copy code
late final SessionNotifier sessionNotifier;

@override
void initState() {
  super.initState();

  sessionNotifier = yourGlobalSessionNotifier; // however you access it

  sessionNotifier.addListener(() {
    final token = sessionNotifier.session?.sessionJwt;
    // ✅ This is your callback: token just updated
    print('Session token updated: $token');

    // Put your side-effect here (send to backend, save, etc.)
  });
}

@override
void dispose() {
  sessionNotifier.removeListener(() {});
  super.dispose();
}
4. Use the current token for API calls When you make API calls:
Copy code
final token = sessionNotifier.session?.sessionJwt;
// use it in Authorization header etc.
Every time Descope updates the session (login or refresh), you call sessionNotifier.setSession(...) → it detects token change → addListener fires → you get your “session token updated” callback. This is the Flutter equivalent of useEffect([sessionToken]).
u
Thanks @Rahul Rijhwani really helps, let me test it out
r
Welcome @Uday Singh Let me know if any further help needed.