init method

  1. @override
Future<void> init()
override

Sets up the explorer and the web3App if they already been initialized.

Implementation

@override
Future<void> init() async {
  _serviceInitialized = false;
  if (!coreUtils.instance.isValidProjectID(_projectId)) {
    _logger.e(
      '[$runtimeType] projectId $_projectId is invalid. '
      'Please provide a valid projectId. '
      'See ${UrlConstants.docsUrl}/appkit/flutter/core/options for details.',
    );
    return;
  }
  if (_status == W3MServiceStatus.initializing ||
      _status == W3MServiceStatus.initialized) {
    return;
  }
  _status = W3MServiceStatus.initializing;
  _notify();

  _registerListeners();

  await _web3App.init();
  await storageService.instance.init();
  await networkService.instance.init();
  await explorerService.instance.init();
  await coinbaseService.instance.init();
  await blockchainService.instance.init();

  _currentSession = await _getStoredSession();
  final isMagic = _currentSession?.sessionService.isMagic == true;
  final isCoinbase = _currentSession?.sessionService.isCoinbase == true;
  if (isMagic || isCoinbase) {
    _currentSelectedChainId = _currentSession!.chainId;
    await _setSesionAndChainData(_currentSession!);
    if (isMagic) {
      await magicService.instance.init();
    }
  } else {
    magicService.instance.init();
  }

  await expirePreviousInactivePairings();

  final wcPairings = _web3App.pairings.getAll();
  final wcSessions = _web3App.sessions.getAll();

  // Loop through all the chain data
  for (final chain in W3MChainPresets.chains.values) {
    for (final event in EventsConstants.allEvents) {
      _web3App.registerEventHandler(
        chainId: chain.namespace,
        event: event,
      );
    }
  }

  // There's a walletconnect session stored
  if (wcSessions.isNotEmpty) {
    await _storeSession(W3MSession(sessionData: wcSessions.first));
    // session should not outlive the pairing
    if (wcPairings.isEmpty) {
      await disconnect();
    } else {
      await _checkSIWEStatus();
    }
  } else {
    // Check for other type of sessions stored
    if (_currentSession != null) {
      if (_currentSession!.sessionService.isCoinbase) {
        final isCbConnected = await coinbaseService.instance.isConnected();
        if (!isCbConnected) {
          await _cleanSession();
        }
      } else if (_currentSession!.sessionService.isMagic) {
        // Every time the app gets killed Magic service will treat the user as disconnected
        // So we will need to treat magic session differently
        final email = _currentSession!.email;
        magicService.instance.setEmail(email);
      } else {
        await _cleanSession();
      }
    }
  }

  // Get the chainId of the chain we are connected to.
  await _selectChainFromStoredId();

  onModalNetworkChange.subscribe(_onNetworkChainRequireSIWE);

  final connected = _web3App.core.relayClient.isConnected;
  _serviceInitialized = connected;
  _status = connected ? W3MServiceStatus.initialized : W3MServiceStatus.error;
  _logger.i('[$runtimeType] initialized');
  _notify();
}