- I'm working with geocoder library in flutter.
- When I get an Address I want to save it in firebase
firestore as an user (object) atribute but to do that I need to
serialize the address (convert it to a map). - Currently, I'm using json_serializable to parse from/to map my User class.
- Because Address is a class from geocoder library I'm having troubles to parse it to/from map because this is handled by json_serializable.
This is my User class:
import 'package:geocoder/model.dart';
import 'package:json_annotation/json_annotation.dart';
import './resource.dart';
part 'user.g.dart';
@JsonSerializable()
class User extends Resource {
User(this.email, this.name);
String email;
String name;
List<Address> addresses;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
- I tried creating a new class called UserAddress whose parent would be Address like following but I'm getting an error
The constructor returns type 'Address' that isn't of expected type 'UserAddress'.dart(invalid_cast_new_expr)
in the named constructor UserAddress.fromJson:
import 'package:geocoder/geocoder.dart';
import 'package:json_annotation/json_annotation.dart';
part 'user-address.g.dart';
@JsonSerializable()
class UserAddress extends Address {
UserAddress();
factory UserAddress.fromJson(Map<String, dynamic> json) =>
Address.fromMap(json);
Map<String, dynamic> toJson() => this.toMap();
}
Please give me some options to achieve parsing to/from map, please.