getTransactionIndex method Null safety
Query the network to find the last transaction from an address @param {String} The address scalar type represents a cryptographic hash used in the ArchEthic network with an identification byte to specify from which algorithm the hash was generated. The Hash appears in a JSON response as Base16 formatted string. The parsed hash will be converted to a binary and any invalid hash with an invalid algorithm or invalid size will be rejected @param {String} The endpoint
Implementation
Future<int> getTransactionIndex(String address, String endpoint) async {
final Completer<int> _completer = Completer<int>();
int _chainLength = 0;
TransactionLastResponse transactionLastResponse = TransactionLastResponse();
final Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Accept': 'application/json',
};
try {
final String _body =
'{"query": "query {lastTransaction(address: \\"$address\\") {chainLength}}"}';
print(_body);
final http.Response responseHttp = await http.post(
Uri.parse(endpoint + '/api'),
body: _body,
headers: requestHeaders);
print(responseHttp.body);
if (responseHttp.statusCode == 200) {
transactionLastResponse =
transactionLastResponseFromJson(responseHttp.body);
if (transactionLastResponse.data != null &&
transactionLastResponse.data!.lastTransaction != null) {
_chainLength =
transactionLastResponse.data!.lastTransaction!.chainLength!;
}
}
} catch (e) {
print('error: ' + e.toString());
}
_completer.complete(_chainLength);
return _completer.future;
}