Skip to content

Commit

Permalink
migrate example to nullsafety (lejard-h#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcjs authored Feb 22, 2022
1 parent 4185d14 commit dbf4272
Show file tree
Hide file tree
Showing 16 changed files with 553 additions and 188 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Please refer to the installation guide at [pub.dev](https://pub.dev/packages/cho

* [json serializable Converter](https://github.com/lejard-h/chopper/blob/master/example/bin/main_json_serializable.dart)
* [built value Converter](https://github.com/lejard-h/chopper/blob/master/example/bin/main_built_value.dart)
* [Angular](https://github.com/lejard-h/chopper/blob/master/example/web/main.dart)

## [Issue Tracker](https://github.com/lejard-h/chopper/issues)

Expand Down
1 change: 0 additions & 1 deletion chopper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ Latest versions:
* [json_serializable Converter](https://github.com/lejard-h/chopper/blob/master/example/bin/main_json_serializable.dart)
* [built_value Converter](https://github.com/lejard-h/chopper/blob/master/example/bin/main_built_value.dart)
* [Angular](https://github.com/lejard-h/chopper/blob/master/example/web/main.dart)
## If you encounter any issues, or need a feature implemented, please visit [Chopper's Issue Tracker on GitHub](https://github.com/lejard-h/chopper/issues).
17 changes: 12 additions & 5 deletions example/bin/main_built_value.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:chopper/chopper.dart';
import 'package:chopper_example/built_value_resource.dart';
import 'package:chopper_example/built_value_serializers.dart';
Expand Down Expand Up @@ -52,16 +53,22 @@ main() async {
}

class BuiltValueConverter extends JsonConverter {
T _deserialize<T>(dynamic value) => jsonSerializers.deserializeWith<T>(
jsonSerializers.serializerForType(T),
value,
);
T? _deserialize<T>(dynamic value) {
final serializer = jsonSerializers.serializerForType(T) as Serializer<T>?;
if (serializer == null) {
throw Exception('No serializer for type ${T}');
}
return jsonSerializers.deserializeWith<T>(
serializer,
value,
);
}

BuiltList<T> _deserializeListOf<T>(Iterable value) => BuiltList(
value.map((value) => _deserialize<T>(value)).toList(growable: false),
);

dynamic _decode<T>(entity) {
dynamic _decode<T>(dynamic entity) {
/// handle case when we want to access to Map<String, dynamic> directly
/// getResource or getMapResource
/// Avoid dynamic or unconverted value, this could lead to several issues
Expand Down
8 changes: 4 additions & 4 deletions example/bin/main_json_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class JsonSerializableConverter extends JsonConverter {

JsonSerializableConverter(this.factories);

T _decodeMap<T>(Map<String, dynamic> values) {
T? _decodeMap<T>(Map<String, dynamic> values) {
/// Get jsonFactory using Type parameters
/// if not found or invalid, throw error or return null
final jsonFactory = factories[T];
Expand All @@ -79,13 +79,13 @@ class JsonSerializableConverter extends JsonConverter {
return jsonFactory(values);
}

List<T> _decodeList<T>(List values) =>
List<T> _decodeList<T>(Iterable values) =>
values.where((v) => v != null).map<T>((v) => _decode<T>(v)).toList();

dynamic _decode<T>(entity) {
dynamic _decode<T>(dynamic entity) {
if (entity is Iterable) return _decodeList<T>(entity);

if (entity is Map) return _decodeMap<T>(entity);
if (entity is Map<String, dynamic>) return _decodeMap<T>(entity);

return entity;
}
Expand Down
22 changes: 0 additions & 22 deletions example/lib/angular_example.dart

This file was deleted.

18 changes: 12 additions & 6 deletions example/lib/built_value_resource.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions example/lib/built_value_resource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class ResourceError

@ChopperApi(baseUrl: "/resources")
abstract class MyService extends ChopperService {
static MyService create([ChopperClient client]) => _$MyService(client);
static MyService create([ChopperClient? client]) => _$MyService(client);

@Get(path: "/{id}/")
Future<Response> getResource(@Path() String id);
Expand All @@ -46,5 +46,5 @@ abstract class MyService extends ChopperService {

@Post()
Future<Response<Resource>> newResource(@Body() Resource resource,
{@Header() String name});
{@Header() String? name});
}
109 changes: 54 additions & 55 deletions example/lib/built_value_resource.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/lib/built_value_serializers.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dbf4272

Please sign in to comment.