The setter ‘target’ isn’t defined for the type ‘LocationModel’.
Try importing the library that defines ‘target’, correcting the name to the name of an existing setter, or defining a setter or field named ‘target’.
below is my code
// Handle current location
if (profileJson.containsKey('current_location')) {
final currentLocationJson =
profileJson['current_location'] as Map<String, dynamic>;
final currentLocationModel =
LocationModel.fromJson(currentLocationJson);
/!! Error here
profileModel.currentLocation.target = currentLocationModel;
}
// Handle home town
if (profileJson.containsKey('home_town')) {
final hometownJson = profileJson['home_town'] as Map<String, dynamic>;
final hometownModel = LocationModel.fromJson(hometownJson);
/!! Error here
profileModel.homeTown.target = hometownModel;
}
**/** No error with this one **/**
profileModel.userModel.target = userModel;
My model example
class LocationModel extends Equatable {
@Id()
int id;
final String? city;
final String? country;
final String? latitude;
final String? longitude;
LocationModel({
this.id = 0,
this.city,
this.country,
this.latitude,
this.longitude,
});
final currentLocation = ToOne<UserProfileModel>();
final homeTown = ToOne<UserProfileModel>();
my profile model
class UserProfileModel extends Equatable {
@Id()
int id;
@JsonKey(name: 'avatar_url')
final String? avatarUrl;
@JsonKey(name: 'current_location')
@Transient()
final LocationModel? currentLocation;
@JsonKey(name: 'home_town')
@Transient()
final LocationModel? homeTown;
final userModel = ToOne<UserModel>();
UserProfileModel({
this.id = 0,
This looks like the LocationModel
and UserProfileModel
are mixed up. Both of them have a currentLocation
field. But one has type ToOne<UserProfileModel>
, the other of LocationModel?
.
Only the ToOne
has the target
method.
1