address_model.dart
class Address {
String placeFormattedAddress;
String placeName;
String placeId;
double latitude;
double longitude;
Address(this.latitude, this.longitude,
this.placeFormattedAddress,
this.placeId, this.placeName);
}
and here is assistant_methods.dart
if (response != "failed") {
placeAddress = response["results"][0].
["formatted_address"];
Address userPickUpAdress = Address();
userPickUpAdress.longitude = position.longitude;
userPickUpAdress.latitude = position.latitude;
userPickUpAdress.placeName = placeAddress;
Provider.of<AppData>(context, listen: false)
.updatePickUpAdress(userPickUpAdress);
}
the error line is on the assitant_methods.dart in line 4, which is when I called out Address(), and in the below code I already intialize
CodePudding user response:
Hi Iamshabell and welcome to SO!
You're trying to use the constructor without any argument on this line :
Address userPickUpAdress = Address();
But on your class, the constructor arguments are mandatory:
Address(this.latitude, this.longitude, this.placeFormattedAddress, this.placeId, this.placeName);
So you need to make them optionals or call the constructor with all the args.
CodePudding user response:
You declared Address as follows:
Address(
this.latitude,
this.longitude,
this.placeFormattedAddress,
this.placeId,
this.placeName,
);
This means that when initializing the address, you HAVE to pass the five arguments, In order to solve this problem, you have to do two things. First, make the arguments optional, so that you don't HAVE to pass them into the constructor.
Address({
this.latitude,
this.longitude,
this.placeFormattedAddress,
this.placeId,
this.placeName,
});
Note the {}
around all of the optional arguments.
This means that you are allowed to not pass any value to the constructor. But perhaps a better solution would be to simply pass the values in the first place.
The second problem you need to fix is the values of your uninitialized variables. What do you think should happen if you read placeId? You never assign it. Should it throw an error (as it did)? Should it be an empty string? should it be null? Ask yourself that question for every single one of the variables.
If the variable should have a default value (like an empty string) you can put this in the constructor:
MyClass({this.myValue: 'default value'});
If the variable should throw an error if you don't pass it, you can keep the variable like you have it or add a required parameter to the constructor.
MyClass(this.myRequiredVariable, {required this.myOtherRequiredVariable});
Finally, if the value should be null. When you declare the variable. Add a question mark after its value to denote it can be null
class MyClass {
String? myNullableString;
}
Finally, it is worth noting that this has as a side effect that if you want to initialize a value from the constructor, you have to pass its name:
MyClass({this.value});
// when initializing
// MyClass(10); // this won't work
MyClass(value: 10); // This will work
If you don't want that, feel free to replace the {}
with []
on the constructor, this will also make it impossible for the required
keyword to be used.
Hope that is enough to solve the problem, but feel free to ask if I wasn't clear on something