Closed dickermoshe closed 2 weeks ago
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';
part 'client_client.g.dart';
@RestApi()
abstract class ClientClient {
factory ClientClient(Dio dio, {String? baseUrl}) = _ClientClient;
/// test upload.
///
/// upload test.
@POST('/test/upload')
Future<void> testUpload({
@Body() File? nullableBody,
});
}
@override
Future<void> testUpload({File? nullableBody}) async {
final _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
queryParameters.removeWhere((k, v) => v == null);
final _headers = <String, dynamic>{};
final _data =
Stream.fromIterable(nullableBody.readAsBytesSync().map((i) => [i])); // This should be checked for null !!
await _dio.fetch<void>(_setStreamType<void>(Options(
method: 'POST',
headers: _headers,
extra: _extra,
)
.compose(
_dio.options,
'/test/upload',
queryParameters: queryParameters,
data: _data,
)
.copyWith(
baseUrl: _combineBaseUrls(
_dio.options.baseUrl,
baseUrl,
))));
}
@override
Future<void> testUpload({File? requiredBody}) async {
final _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
queryParameters.removeWhere((k, v) => v == null);
final _headers = <String, dynamic>{};
final _data = requiredBody == null
? null
: Stream.fromIterable(requiredBody.readAsBytesSync().map((i) => [i]));
await _dio.fetch<void>(_setStreamType<void>(Options(
method: 'POST',
headers: _headers,
extra: _extra,
)
.compose(
_dio.options,
'/test/upload',
queryParameters: queryParameters,
data: _data,
)
.copyWith(
baseUrl: _combineBaseUrls(
_dio.options.baseUrl,
baseUrl,
))));
}
Thanks man!
@dickermoshe
Can you fix the unit failure?
just pull the latest code and run dart run test
in generator
The PR caused build failure.
I haven't looked at this PR in a while. I'll try to look at it again today.
Currently there is no null checking when retrofit works on a request body. https://github.com/trevorwang/retrofit.dart/issues/662
This small refactor separates the process of setting the data variable into 3 parts
final data =
body.map((e) => e.toJson()).toList()
data.removeWhere((k, v) => v == null);
This way we can assign the data value using a ternary expression:
and also run the Post Assignment in a type safe manner:
This change is completely backwards compatible, using this with a required, non-nullable body results in the exactly same code.