1
resposta

NoSuchMethodError: Class 'TimeoutException' has no instance getter 'statusCode'

Olá, boa noite. Os meus erros sempre voltar a mesma coisa. Eu vi que tinha tópico aberto de alguém com o mesmo erro, tentei a solução proposta e não funcionou. Por acaso alguém passou pela mesma coisa?

I/flutter ( 8682): ----------------FIREBASE CRASHLYTICS----------------
I/flutter ( 8682): NoSuchMethodError: Class 'TimeoutException' has no instance getter 'statusCode'.
I/flutter ( 8682): Receiver: Instance of 'TimeoutException'
I/flutter ( 8682): Tried calling: statusCode
I/flutter ( 8682): ----------------------------------------------------
E/flutter ( 8682): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Invalid argument(s) (onError): The error handler of Future.catchError must return a value of the future's type
E/flutter ( 8682): #0      _FutureListener.handleError (dart:async/future_impl.dart:181:7)
E/flutter ( 8682): #1      Future._propagateToListeners.handleError (dart:async/future_impl.dart:778:47)
E/flutter ( 8682): #2      Future._propagateToListeners (dart:async/future_impl.dart:799:13)
E/flutter ( 8682): #3      Future._completeError (dart:async/future_impl.dart:574:5)
E/flutter ( 8682): #4      _completeOnAsyncError (dart:async-patch/async_patch.dart:287:13)
E/flutter ( 8682): #5      TransactionWebClient.save (package:bytebank/database/http/webclients/transaction_webclient.dart)
E/flutter ( 8682): <asynchronous suspension>

O meu código _send:

Future<Transaction> _send(Transaction transactionCreated, String password,
      BuildContext context) async {
    setState(() {
      _sending = true;
    });
    final Transaction transaction =
        await _webClient.save(transactionCreated, password).catchError((e) {
      FirebaseCrashlytics.instance.setCustomKey('exception', e.toString());
      FirebaseCrashlytics.instance
          .setCustomKey('http_body', transactionCreated.toString());
      FirebaseCrashlytics.instance.recordError(e, null);

      _showFailureMessage(context, message: e.message);
    }, test: (e) => e is HttpException).catchError((e) {
      FirebaseCrashlytics.instance.setCustomKey('exception', e.toString());
      FirebaseCrashlytics.instance.setCustomKey('http_code', e.statusCode);
      FirebaseCrashlytics.instance
          .setCustomKey('http_body', transactionCreated.toString());
      FirebaseCrashlytics.instance.recordError(e, null);

      _showFailureMessage(context,
          message: 'timeout submitting the transaction');
    }, test: (e) => e is TimeoutException).catchError((e) {
      FirebaseCrashlytics.instance.setCustomKey('exception', e.toString());
      FirebaseCrashlytics.instance
          .setCustomKey('http_body', transactionCreated.toString());
      FirebaseCrashlytics.instance.recordError(e, null);

      _showFailureMessage(context);
    }).whenComplete(() {
      setState(() {
        _sending = false;
      });
    });
    return transaction;
  }

O código do meu TransactionWebClient:

class TransactionWebClient {
  Future<List<Transaction>> findAll() async {
    final Response response = await client.get(Uri.parse(baseURL));
    final List<dynamic> decodedJson = jsonDecode(response.body);
    return decodedJson
        .map((dynamic json) => Transaction.fromJson(json))
        .toList();
  }

  Future<Transaction> save(Transaction transaction, String password) async {
    final String transactionJson = jsonEncode(transaction.toJson());

    await Future.delayed(Duration(seconds: 2));
    final Response response = await client.post(Uri.parse(baseURL),
        headers: {
          'Content-type': 'application/json',
          'password': password,
        },
        body: transactionJson);

    if (response.statusCode == 200) {
      return Transaction.fromJson(jsonDecode(response.body));
    }

    throw HttpException(_getMessage(response.statusCode), response.statusCode);
  }

  String? _getMessage(int statusCode) {
    if (_statusCodeResponses.containsKey(statusCode)) {
      return _statusCodeResponses[statusCode];
    }
    return 'unknown error';
  }

  static final Map<int, String> _statusCodeResponses = {
    400: 'there was an error submitting transaction',
    401: 'authentication failed',
    409: 'transaction already exists'
  };
}

class HttpException implements Exception {
  final String? message;
  final int? statusCode;

  HttpException(this.message, this.statusCode);
}
1 resposta

Bom dia, Mikael! Tudo bem?

Primeiramente quero te pedir desculpas pela demora em dar um retorno.

Acredito que você esteja apenas colocando a instrução no lugar errado, tente colocar no catchError acima, referente ao erro de HttpException. Ficará da seguinte maneira:

Future<Transaction?> _send(Transaction transactionCreated, String password,
      BuildContext context) async {
    final Transaction? transaction =
        await _webClient.save(transactionCreated, password).catchError((e) {
      if (FirebaseCrashlytics.instance.isCrashlyticsCollectionEnabled) {
        FirebaseCrashlytics.instance.setCustomKey('exception', e.toString());
        FirebaseCrashlytics.instance
            .setCustomKey('http_body', transactionCreated.toString());
        // Chamada para o StatusCode aqui:
        FirebaseCrashlytics.instance.setCustomKey('http_code', e.statusCode);
        FirebaseCrashlytics.instance.recordError(e, null);
      }

      _showFailureMessage(context, e.message);
      // print(e);
    }, test: (e) => e is HttpException).catchError((e) {
      if (FirebaseCrashlytics.instance.isCrashlyticsCollectionEnabled) {
        FirebaseCrashlytics.instance.setCustomKey('exception', e.toString());
        FirebaseCrashlytics.instance
            .setCustomKey('http_body', transactionCreated.toString());

        FirebaseCrashlytics.instance.recordError(e, null);
      }
      _showFailureMessage(context, 'timeout submitting the transaction');
      // print(e);
    }, test: (e) => e is TimeoutException).catchError((e) {
      if (FirebaseCrashlytics.instance.isCrashlyticsCollectionEnabled) {
        FirebaseCrashlytics.instance.setCustomKey('exception', e.toString());
        FirebaseCrashlytics.instance
            .setCustomKey('http_body', transactionCreated.toString());
        FirebaseCrashlytics.instance.recordError(e, null);
      }
      _showFailureMessage(context, e.message);
      // print(e);
    });
    return transaction;
  }

E lembre-se também que o HttpException fomos nós mesmos que criamos, então precisamos criar a variável statusCode lá também. A classe HttpException, que está localizada dentro de transaction_webclient.dart, ficará da seguinte forma:

class HttpException implements Exception {
  final String? message;
  final int? statusCode;
  HttpException(this.message, this.statusCode);
}

Com isso terá um problema no código acima da classe, quando lançamos a exceção de HttpException, que pode ser resolvido apenas passando o response.statusCode na chamada da exceção:

throw HttpException(_getMessage(response.statusCode), response.statusCode);

Espero que isso tenha te ajudado. Bons estudos!

Caso este post tenha lhe ajudado, por favor, marcar como solucionado. ✓