Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonConverter Error #1022

Open
oyen-bright opened this issue Dec 19, 2023 · 2 comments
Open

JsonConverter Error #1022

oyen-bright opened this issue Dec 19, 2023 · 2 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@oyen-bright
Copy link

`part of 'vehicle_cubit.dart';

@freezed
class VehicleState with _$VehicleState {
const factory VehicleState.initial(
@VehicleQueryOptionsConverter() VehicleQueryOptions myResponse) =
_Initial;

const factory VehicleState.loading({
required List vehicles,
required @VehicleQueryOptionsConverter() VehicleQueryOptions queryOptions,
}) = _Loading;

const factory VehicleState.loaded({
required List vehicles,
required List filteredVehicles,
required VehicleQueryOptions queryOptions,
}) = _Loaded;

const factory VehicleState.error({
String? message,
List? vehicles,
required VehicleQueryOptions queryOptions,
}) = _Error;

factory VehicleState.fromJson(Map<String, dynamic> json) =>
_$VehicleStateFromJson(json);
}

class VehicleQueryOptionsConverter
implements JsonConverter<VehicleQueryOptions, Map<String, dynamic>> {
const VehicleQueryOptionsConverter();

@OverRide
VehicleQueryOptions fromJson(Map<String, dynamic> json) =>
VehicleQueryOptions.fromMap(json);

@OverRide
Map<String, dynamic> toJson(VehicleQueryOptions options) =>
options.toJson() as Map<String, dynamic>;
}
`
How do i use the VehicleQueryOptionsConverter the use case is not the doc.

@oyen-bright
Copy link
Author

// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';

class VehicleQueryOptions {
final List? brand;
final List? type;
final List? capacity;
final String? min;
final String? max;

VehicleQueryOptions({
this.brand,
this.type,
this.capacity,
this.min,
this.max,
});

Map<String, dynamic> toMap() {
return <String, dynamic>{
'brand': brand,
'type': type,
'capacity': capacity,
'min': min,
'max': max,
};
}

factory VehicleQueryOptions.fromMap(Map<String, dynamic> map) {
return VehicleQueryOptions(
brand: map['brand'] != null
? List.from((map['brand'] as List))
: null,
type: map['type'] != null
? List.from((map['type'] as List))
: null,
capacity: map['capacity'] != null
? List.from((map['capacity'] as List))
: null,
min: map['min'] != null ? map['min'] as String : null,
max: map['max'] != null ? map['max'] as String : null,
);
}

String toJson() => json.encode(toMap());

factory VehicleQueryOptions.fromJson(String source) =>
VehicleQueryOptions.fromMap(json.decode(source) as Map<String, dynamic>);
}

@rrousselGit
Copy link
Owner

What's the problem? You only shared code. I'm not aware of what the issue is

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants