sendCommand method Null safety

  1. @override
Future<Map<String, dynamic>> sendCommand(
  1. Command command
)
override

Sends command to the Flutter Driver extensions. This must be implemented by subclass.

See also:

Implementation

@override
Future<Map<String, dynamic>> sendCommand(Command command) async {
  final Map<String, dynamic> response;
  final Object? data;
  final Map<String, String> serialized = command.serialize();
  _logCommunication('>>> $serialized');
  try {
    data = await _connection.sendCommand("window.\$flutterDriver('${jsonEncode(serialized)}')", command.timeout);

    // The returned data is expected to be a string. If it's null or anything
    // other than a string, something's wrong.
    if (data is! String) {
      throw _createMalformedExtensionResponseError(data);
    }

    final Object? decoded = json.decode(data);
    if (decoded is! Map<String, dynamic>) {
      throw _createMalformedExtensionResponseError(data);
    } else {
      response = decoded;
    }

    _logCommunication('<<< $response');
  } on DriverError catch(_) {
    rethrow;
  } catch (error, stackTrace) {
    throw DriverError(
      'FlutterDriver command ${command.runtimeType} failed due to a remote error.\n'
      'Command sent: ${jsonEncode(serialized)}',
      error,
      stackTrace
    );
  }

  final Object? isError = response['isError'];
  final Object? responseData = response['response'];
  if (isError is! bool?) {
    throw _createMalformedExtensionResponseError(data);
  } else if (isError ?? false) {
    throw DriverError('Error in Flutter application: $responseData');
  }

  if (responseData is! Map<String, dynamic>) {
    throw _createMalformedExtensionResponseError(data);
  }
  return responseData;
}