@@ -262,7 +262,9 @@ class WalletConnectModalService extends ChangeNotifier
262262 }
263263
264264 @override
265- Future <void > disconnect () async {
265+ Future <void > disconnect ({
266+ bool disconnectAllSessions = true ,
267+ }) async {
266268 checkInitialized ();
267269
268270 // If we don't have a session, disconnect automatically and notify listeners
@@ -273,31 +275,14 @@ class WalletConnectModalService extends ChangeNotifier
273275 return ;
274276 }
275277
276- // Disconnect both the pairing and session
277- await web3App! .disconnectSession (
278- topic: session! .pairingTopic,
279- // ignore: prefer_const_constructors
280- reason: WalletConnectError (
281- code: 0 ,
282- message: 'User disconnected' ,
283- ),
284- );
285- // Disconnecting the session will produce the onSessionDisconnect callback
286- await web3App! .disconnectSession (
287- topic: session! .topic,
288- // ignore: prefer_const_constructors
289- reason: WalletConnectError (
290- code: 0 ,
291- message: 'User disconnected' ,
292- ),
293- );
294-
295- // As a failsafe (If the session is expired for example), set the session to null and notify listeners
296- if (_session != null ) {
297- _isConnected = false ;
298- _address = '' ;
299- _session = null ;
300- notifyListeners ();
278+ // If we want to disconnect all sessions, loop through them and disconnect them
279+ if (disconnectAllSessions) {
280+ for (final SessionData session in web3App! .sessions.getAll ()) {
281+ await disconnectSession (session);
282+ }
283+ } else {
284+ // Disconnect the session
285+ await disconnectSession (_session! );
301286 }
302287 }
303288
@@ -442,6 +427,36 @@ class WalletConnectModalService extends ChangeNotifier
442427
443428 ////// Private methods //////
444429
430+ @protected
431+ Future <void > disconnectSession (SessionData toDisconnect) async {
432+ // Disconnect both the pairing and session
433+ await web3App! .disconnectSession (
434+ topic: toDisconnect.pairingTopic,
435+ // ignore: prefer_const_constructors
436+ reason: WalletConnectError (
437+ code: 0 ,
438+ message: 'User disconnected' ,
439+ ),
440+ );
441+ // Disconnecting the session will produce the onSessionDisconnect callback
442+ await web3App! .disconnectSession (
443+ topic: toDisconnect.topic,
444+ // ignore: prefer_const_constructors
445+ reason: WalletConnectError (
446+ code: 0 ,
447+ message: 'User disconnected' ,
448+ ),
449+ );
450+
451+ // As a failsafe (If the session is expired for example), set the session to null and notify listeners
452+ if (_session != null && session! .topic == toDisconnect.topic) {
453+ _isConnected = false ;
454+ _address = '' ;
455+ _session = null ;
456+ notifyListeners ();
457+ }
458+ }
459+
445460 @protected
446461 Redirect ? constructRedirect () {
447462 if (session == null ) {
0 commit comments