Skip to content

Commit e6803a0

Browse files
committed
fix: pre initialize headers and params on request data
1 parent 8f2be2d commit e6803a0

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

example/lib/main.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ class WeatherApiInterceptor implements InterceptorContract {
302302
@override
303303
Future<RequestData> interceptRequest({required RequestData data}) async {
304304
try {
305-
data.params!['appid'] = OPEN_WEATHER_API_KEY;
306-
data.params!['units'] = 'metric';
307-
data.headers![HttpHeaders.contentTypeHeader] = "application/json";
305+
data.params['appid'] = OPEN_WEATHER_API_KEY;
306+
data.params['units'] = 'metric';
307+
data.headers[HttpHeaders.contentTypeHeader] = "application/json";
308308
} catch (e) {
309309
print(e);
310310
}

lib/models/request_data.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import 'package:http_interceptor/utils.dart';
77
class RequestData {
88
Method method;
99
String baseUrl;
10-
Map<String, String>? headers;
11-
Map<String, String>? params;
10+
Map<String, String> headers;
11+
Map<String, String> params;
1212
dynamic? body;
1313
Encoding? encoding;
1414

1515
RequestData({
1616
required this.method,
1717
required this.baseUrl,
18-
this.headers,
19-
this.params,
18+
Map<String, String>? headers,
19+
Map<String, String>? params,
2020
this.body,
2121
this.encoding,
22-
});
22+
}) : headers = headers ?? {},
23+
params = params ?? {};
2324

2425
String get url => addParametersToStringUrl(baseUrl, params);
2526

@@ -44,7 +45,7 @@ class RequestData {
4445

4546
Request request = new Request(methodToString(method), reqUrl);
4647

47-
if (headers != null) request.headers.addAll(headers!);
48+
request.headers.addAll(headers);
4849
if (encoding != null) request.encoding = encoding!;
4950
if (body != null) {
5051
if (body is String) {

0 commit comments

Comments
 (0)