callApi method
Call native method
Implementation
Future<ReturnParams> callApi(CallParams params) async {
try {
logger.info('callApi:', [params.toJson()]);
final result = await _methodChannel
.invokeMethod('callApi', {'params': jsonEncode(params.toJson())});
// Check and handle return value
if (result == null) {
throw Exception('Native return value is empty');
}
// Ensure result is string type
String jsonString;
if (result is String) {
jsonString = result;
} else {
jsonString = jsonEncode(result);
}
try {
final Map<String, dynamic> json = jsonDecode(jsonString);
logger.info('call Api result:', [json]);
return ReturnParams.fromJson(json);
} catch (e) {
logger.error('Failed to parse JSON:', [jsonString]);
throw Exception('Invalid return data format: $e');
}
} catch (e) {
logger.error('Failed to call native method:', [e]);
rethrow;
}
}