@@ -39,22 +39,25 @@ class _ConnectNetworkPageState extends State<ConnectNetworkPage>
3939 WidgetsBinding .instance.addPostFrameCallback ((_) {
4040 _service = Web3ModalProvider .of (context).service;
4141 _service? .onModalError.subscribe (_errorListener);
42- _service! .web3App! .onSessionEvent.subscribe (_onSessionEvent);
43- _service! .web3App! .core.relayClient.onRelayClientMessage.subscribe (
44- _onRelayClientMessage,
45- );
4642 setState (() {});
47- Future .delayed (const Duration (milliseconds: 300 ), () {
48- _connect ();
49- });
43+ Future .delayed (const Duration (milliseconds: 300 ), () => _connect ());
5044 });
5145 }
5246
53- void _connect () {
47+ void _connect () async {
5448 errorEvent = null ;
5549 _service! .launchConnectedWallet ();
56- _service! .requestSwitchToChain (widget.chainInfo);
57- setState (() {});
50+ try {
51+ await _service! .requestSwitchToChain (widget.chainInfo);
52+ final chainId = widget.chainInfo.chainId;
53+ if (W3MChainPresets .chains.containsKey (chainId)) {
54+ Future .delayed (const Duration (milliseconds: 300 ), () {
55+ widgetStack.instance.pop ();
56+ });
57+ }
58+ } catch (e) {
59+ setState (() {});
60+ }
5861 }
5962
6063 @override
@@ -68,38 +71,10 @@ class _ConnectNetworkPageState extends State<ConnectNetworkPage>
6871 }
6972 }
7073
71- void _onSessionEvent (SessionEvent ? event) async {
72- if (! mounted) return ;
73- if (event? .name == EventsConstants .chainChanged) {
74- debugPrint ('[$runtimeType ] _onSessionEvent $event ' );
75- final chainId = event? .data.toString () ?? '' ;
76- if (W3MChainPresets .chains.containsKey (chainId)) {
77- _service? .web3App? .onSessionEvent.unsubscribe (_onSessionEvent);
78- widgetStack.instance.pop ();
79- }
80- }
81- }
82-
83- void _onRelayClientMessage (MessageEvent ? event) async {
84- if (! mounted) return ;
85- if (event != null ) {
86- final payloadString = await _service! .web3App! .core.crypto.decode (
87- event.topic,
88- event.message,
89- );
90- if (payloadString == null ) return ;
91- debugPrint ('[$runtimeType ] payloadString $payloadString ' );
92- }
93- }
94-
9574 void _errorListener (ModalError ? event) => setState (() => errorEvent = event);
9675
9776 @override
9877 void dispose () {
99- _service? .web3App? .core.relayClient.onRelayClientMessage.unsubscribe (
100- _onRelayClientMessage,
101- );
102- _service? .web3App? .onSessionEvent.unsubscribe (_onSessionEvent);
10378 _service? .onModalError.unsubscribe (_errorListener);
10479 WidgetsBinding .instance.removeObserver (this );
10580 super .dispose ();
0 commit comments