작성
·
1.1K
0
test/widget_test.dart:16:35: Error: The argument type 'String' can't be assigned to the parameter type 'Uri'.
- 'Uri' is from 'dart:core'.
var response = await http.get('https://api.airvisual.com/v2/nearest_city?key=05dad879-fd62-46f1-bdcd-7f4e022f6b1f');
^
Failed to load "/Users/goseung-wan/Documents/flutter_dust/test/widget_test.dart": Compilation failed for testPath=/Users/goseung-wan/Documents/flutter_dust/test/widget_test.dart
이런식으로 나오는데 어떤 부분을 수정하면 될까요??
url에 숫자가 포함되어서 그런건지..
답변 2
1
최신 버전으로 하시고 계신다면 문서를 확인해야 하는데요.
https://pub.dev/packages/http
버전업이 되면서 사용방법이 변경되어서 Uri.parse( ) 로 URL을 감싸줘야 합니다.
var response = await http.get(Uri.parse('https://api.airvisual.com/v2/nearest_city?key=05dad879-fd62-46f1-bdcd-7f4e022f6b1f'));
로 하시면 됩니다.
0
import 'package:flutter/material.dart';
import 'package:flutter_dust/models/AirResult.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_dust/main.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
test('http 통신 테스트', () async {
var response = await http.get(Uri.parse(
'https://api.iqair.com/v2/nearest_city?key=b41f3555-5530-428c-84b7-da354eb5170c'));
expect(response.statusCode, 200);
AirResult result = AirResult.fromJson(json.decode(response.body));
expect(result.status, 'success');
});
}
HandshakeException: Handshake error in client (OS Error:
CERTIFICATE_VERIFY_FAILED: Hostname mismatch(../../third_party/boringssl/src/ssl/handshake.cc:393))
에러가 뜸닙니다.
인증서 문제인데요. 임시적으로 우회할 수 있습니다. 테스트시에만 사용해야합니다.
참고 : https://stackoverflow.com/questions/54285172/how-to-solve-flutter-certificate-verify-failed-error-while-performing-a-post-req
test('http 통신 테스트', () async {
HttpOverrides.global = MyHttpOverrides();
var response = await get(Uri.parse(
'https://api.iqair.com/v2/nearest_city?key=b41f3555-5530-428c-84b7-da354eb5170c'));
expect(response.statusCode, 200);
AirResult result = AirResult.fromJson(json.decode(response.body));
expect(result.status, 'success');
});
}
class MyHttpOverrides extends HttpOverrides{
@override
HttpClient createHttpClient(SecurityContext? context) {
return super.createHttpClient(context)
..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
}
}
감사합니다!!