I have this code:
RaisedButton(
child: Text('Add'),
onPressed: () async {
if (_formKey.currentState.validate()) {
DocumentReference docRef =
await DatabaseServices().addUserProperty(
user.userUid,
_currentPropertyName,
_currentPropertyNotes,
_currentPropertyZone,
_currentPropertyAddress `enter code here`,
_currentPropertyLandArea,
_currentPropertyDatePurchased,
_currentPropertyRatesBillingCode,
_currentPropertyInsurancePolicy,
_currentPropertyInsuranceSource,
_currentPropertyInsuranceExpiryDate `enter code here`,
_currentPropertyLegalDescription,
_currentPropertyValuation,
_currentPropertyValuationSource,
false,
Timestamp.now(),
Timestamp.now(),
);
await DatabaseServices().addPropertyUnit(
user.userUid,
docRef.documentID,
'Single unit',
'',
'',
0,
false,
false,
Timestamp.now(),
Timestamp.now(),
);
Navigator.pop(context);
}
})
where I am trying to use 'docRef.documentID' from the just generated documentId for addUserProperty. I want to save this as a field in the addPropertyUnit. I get the error 'The getter 'documentID' was called on null' on the line docRef.documentID. What should I do?
Thanks for the suggestion ralemos. I did place the second await etc inside a .then(). I now have this code:
RaisedButton(
child: Text('Add'),
onPressed: () async {
if (_formKey.currentState.validate()) {
DocumentReference docRef = await DatabaseServices()
.addUserProperty(
user.userUid,
_currentPropertyName,
_currentPropertyNotes,
_currentPropertyZone,
_currentPropertyAddress,
_currentPropertyLandArea,
_currentPropertyDatePurchased,
_currentPropertyRatesBillingCode,
_currentPropertyInsurancePolicy,
_currentPropertyInsuranceSource,
_currentPropertyInsuranceExpiryDate,
_currentPropertyLegalDescription,
_currentPropertyValuation,
_currentPropertyValuationSource,
false,
Timestamp.now(),
Timestamp.now(),
)
.then((docRef) async {
await DatabaseServices().addPropertyUnit(
user.userUid,
'nnnnnnnnnn',
// docRef.documentID,
'Single unit',
'',
'',
0,
false,
false,
Timestamp.now(),
Timestamp.now(),
);
return null;
});
// print('docRef: ${docRef.documentID}');
Navigator.pop(context);
}
})
which works in that it saves a new PropertyUnit but how do I pass the docRef.documentID from .addUserProperty to .addPropertyUnit()? Currently it is just saving 'nnnnnnnnnnn' instead. If I define 'DocumentReference docRef;' just below the 'Widget build(BuildContext context) {', docRef.documentID is available in .addPropertyUnit but still get a runtime error of ' The getter 'documentID' was called on null'.
The code for addPropertyUnit is:
// add a unit to a property
Future addPropertyUnit(
String userUid,
String unitPropertyUid,
String unitName,
String unitNotes,
String unitLeaseDescription,
num unitArea,
bool unitResidential,
bool unitArchived,
Timestamp unitRecordCreatedDateTime,
Timestamp unitRecordLastEdited,
) async {
return await userUnitCollection.document().setData(
{
'userUid': userUid,
'propertyUid': unitPropertyUid,
'unitName': unitName,
'unitNotes': unitNotes,
'unitLeaseDescription': unitLeaseDescription,
'unitArea': unitArea,
'unitResidential': unitResidential,
'unitArchived': unitArchived,
'unitRecordCreatedDateTime': unitRecordCreatedDateTime,
'unitRecordLastEdited': unitRecordLastEdited,
},
);
}
And addUserProperty:
// add a property
Future addUserProperty(
String userUid,
String propertyName,
String propertyNotes,
String propertyZone,
String propertyAddress,
double propertyLandArea,
DateTime propertyDatePurchased,
String propertyRatesBillingCode,
String propertyInsurancePolicy,
String propertyInsuranceSource,
DateTime propertyInsuranceExpiryDate,
String propertyLegalDescription,
double propertyValuation,
String propertyValuationSource,
bool propertyArchived,
Timestamp propertyRecordCreatedDateTime,
Timestamp propertyRecordLastEdited,
) async {
return await userPropertyCollection.document().setData(
{
'userUid': userUid,
'propertyName': propertyName,
'propertyNotes': propertyNotes,
'propertyZone': propertyZone,
'propertyAddress': propertyAddress,
'propertyLandArea': propertyLandArea,
'propertyDatePurchased': propertyDatePurchased,
'propertyRatesBillingCode': propertyRatesBillingCode,
'propertyInsurancePolicy': propertyInsurancePolicy,
'propertyInsuranceSource': propertyInsuranceSource,
'propertyInsuranceDate': propertyInsuranceExpiryDate,
'propertyLegalDescription': propertyLegalDescription,
'propertyMarketValuation': propertyValuation,
'propertyMarketValuationSource': propertyValuationSource,
'propertyArchived': propertyArchived,
'propertyRecordCreatedDateTime': propertyRecordCreatedDateTime,
'propertyRecordLastEdited': propertyRecordLastEdited,
},
);
}