initialize method

Future<void> initialize()

Implementation

Future<void> initialize() async {
  if (!config.isActive || _active) return;
  if (config.endpoint.scheme != 'https' &&
      !(config.allowInsecureLocal &&
          [
            'localhost',
            '127.0.0.1',
            '10.0.2.2',
          ].contains(config.endpoint.host))) {
    throw ArgumentError(
      'HTTPS required; explicitly allow localhost for development',
    );
  }
  _active = true;
  _clock.start();
  WidgetsBinding.instance.addObserver(this);
  channel.setMethodCallHandler((call) async {
    if (call.method == 'submitEditor') {
      final args = Map<String, dynamic>.from(call.arguments as Map);
      final handler = _submitEditor;
      if (!_active ||
          handler == null ||
          args['sessionId'] != _editorSession) {
        throw PlatformException(
          code: 'editor_closed',
          message: 'Report editor is no longer active',
        );
      }
      return handler(
        Map<String, dynamic>.from(
          jsonDecode(args['payloadJson'] as String) as Map,
        ),
      );
    }
    if (call.method == 'screenshotDetected' &&
        _active &&
        isDeviceApproved &&
        !isPresentingUi &&
        _foreground) {
      await showReport();
    }
  });
  if (_requiresDeviceApproval) {
    deviceRegistration.addListener(_deviceChanged);
    unawaited(deviceRegistration.initialize());
    _devicePoll = Timer.periodic(const Duration(seconds: 30), (_) {
      if (_active && _foreground) unawaited(deviceRegistration.refresh());
    });
  }
  await channel.invokeMethod('initialize', {'isActive': isDeviceApproved});
}