getNonce method Null safety

Future<int> getNonce(
  1. String accountId,
  2. String contractId
)

Helper method to get the nonce for a given accountId for the contract specified by the contractId. Used for contract auth.

Implementation

Future<int> getNonce(String accountId, String contractId) async {
  XdrLedgerKey ledgerKey = XdrLedgerKey(XdrLedgerEntryType.CONTRACT_DATA);
  ledgerKey.contractID = XdrHash(Util.hexToBytes(contractId));
  Address address = Address.forAccountId(accountId);
  XdrSCVal nonceKeyVal = XdrSCVal.forNonceKeyWithAddress(address);
  ledgerKey.contractDataKey = nonceKeyVal;
  GetLedgerEntryResponse response =
      await getLedgerEntry(ledgerKey.toBase64EncodedXdrString());
  if (!response.isErrorResponse &&
      response.ledgerEntryDataXdr != null &&
      response.ledgerEntryDataXdr!.contractData != null) {
    XdrSCVal val = response.ledgerEntryDataXdr!.contractData!.val;
    if (val.u64 != null) {
      return val.u64!.uint64;
    }
  }
  return 0;
}