在Dart Flutter中将文档添加到Firestore后如何直接获取文档ID

史塔伊克

我有这个代码:

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);
        }
    })

我试图在 addUserProperty 中使用刚刚生成的 documentId 中的“docRef.documentID”。我想将其保存为 addPropertyUnit 中的一个字段。我在 docRef.documentID 行上收到错误“The getter 'documentID' was called on null”。我应该怎么办?

感谢拉勒莫斯的建议。我确实将第二个等待等放在 .then() 中。我现在有这个代码:

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);
                  }
                })  

它的工作原理是它保存了一个新的 PropertyUnit,但是如何将 docRef.documentID 从 .addUserProperty 传递到 .addPropertyUnit()?目前它只是保存'nnnnnnnnnnn'。如果我定义 'DocumentReference docRef;' 在 'Widget build(BuildContext context) {' 的正下方,docRef.documentID 在 .addPropertyUnit 中可用,但仍然出现运行时错误“The getter 'documentID' was called on null”。

addPropertyUnit 的代码是:

// 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,
      },
    );
  }

并添加用户属性:

 // 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,
      },
    );
  }
莫雷兹

使用下面的代码。我相信它可以工作。

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(),
                );
                  await DatabaseServices().addPropertyUnit(
                    user.userUid,
                    docRef.documentID,
                    'Single unit',
                    '',
                    '',
                    0,
                    false,
                    false,
                    Timestamp.now(),
                    Timestamp.now());
               print('docRef: ${docRef.documentID}');

                Navigator.pop(context);
              }
            })  

我们的代码之间的区别在于,这段代码首先等待 addUserProperty() 完成以获取 docRef 对象,然后当 docRef 不再为 null 时,它会运行 addPropertyUnit() 函数,这样,docRef 就不会为 null。

更新的答案:

使用下面的代码为您添加用户属性:

    // add a property
  Future< DocumentReference> addUserProperty( //the return type has changed to DocumentReference
    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 {
    DocumentReference document = userPropertyCollection.document(); //new document is created here

     await document.setData( // document data is set here.
      {
        '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,
      },
    );
return document; // returns the document after setting the data finishes so in this way, your docRef must not be null anymore.
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章