init method

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

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

Implementation

@override
Future<void> init() async {
  _serviceInitialized = false;
  if (!CoreUtils.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 == ReownAppKitModalStatus.initializing ||
      _status == ReownAppKitModalStatus.initialized) {
    return;
  }
  _status = ReownAppKitModalStatus.initializing;
  _notify();

  _registerListeners();

  await _appKit.init();
  await networkService.instance.init();
  await explorerService.instance.init();
  await coinbaseService.instance.init();
  await blockchainService.instance.init();

  _currentSession = await _getStoredSession();
  _currentSelectedChainId = _getStoredChainId(null);
  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 = _appKit.pairings.getAll();
  final wcSessions = _appKit.sessions.getAll();

  // Loop through all the chain data
  final allNetworks = ReownAppKitModalNetworks.getNetworks(
    CoreConstants.namespace,
  );
  for (final chain in allNetworks) {
    for (final event in EventsConstants.allEvents) {
      _appKit.registerEventHandler(
        chainId: '${CoreConstants.namespace}:${chain.chainId}',
        event: event,
      );
    }
  }

  // There's a session stored
  if (wcSessions.isNotEmpty) {
    await _storeSession(
        ReownAppKitModalSession(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 = _appKit.core.relayClient.isConnected;
  _serviceInitialized = connected;
  _status = connected
      ? ReownAppKitModalStatus.initialized
      : ReownAppKitModalStatus.error;
  _logger.i('[$runtimeType] initialized');
  _notify();
}