getTransactionIndex function Null safety

Future<int> getTransactionIndex(
  1. String address,
  2. String endpoint
)

Implementation

Future<int> getTransactionIndex(String address, String endpoint) async {
  Completer<int> _completer = new Completer<int>();
  int _chainLength = 0;
  TransactionLastResponse transactionLastResponse =
      new TransactionLastResponse();
  final Map<String, String> requestHeaders = {
    'Content-type': 'application/json',
    'Accept': 'application/json',
  };
  try {
    String _body =
        '{"query": "query {lastTransaction(address: \\"$address\\") {chainLength}}"}';
    print(_body);
    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;
}