2
respostas

E/flutter ( 6322): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: NoSuchMethodError: The method 'toHttpRequest' was called on null. E/flutter ( 6322): Receiver: null E/flutter ( 6322): Tried calling: toHttpRequest()

Estou tendo esse erro ao tentar utilizar o Interceptor no flutter: Erro: I/flutter ( 6322): Request I/flutter ( 6322): url: http://172.18.0.1:8080/transactions I/flutter ( 6322): headers: {} I/flutter ( 6322): body: E/flutter ( 6322): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: NoSuchMethodError: The method 'toHttpRequest' was called on null. E/flutter ( 6322): Receiver: null E/flutter ( 6322): Tried calling: toHttpRequest() E/flutter ( 6322): #0 HttpClientWithInterceptor.attemptRequest (package:httpinterceptor/http_client_with_interceptor.dart:209:9) E/flutter ( 6322): E/flutter ( 6322): #1 HttpClientWithInterceptor.sendUnstreamed (package:httpinterceptor/http_client_with_interceptor.dart:167:26) E/flutter ( 6322): #2 HttpClientWithInterceptor.get (package:http_interceptor/http_client_with_interceptor.dart:75:7) E/flutter ( 6322): #3 findAll (package:bytebankpersistence/http/webclient.dart:25:42) E/flutter ( 6322): #4 main (package:bytebankpersistence/main.dart:8:3)

import 'package:http/http.dart';
import 'package:http_interceptor/http_interceptor.dart';

class LoggingInterceptor implements InterceptorContract {
  @override
  Future<RequestData> interceptRequest({RequestData data}) {
    print('Request');
    print('url: ${data.url}');
    print('headers: ${data.headers}');
    print('body: ${data.body}');
  }

  @override
  Future<ResponseData> interceptResponse({ResponseData data}) async {
    print('Response');
    print('status code: ${data.statusCode}');
    print('headers: ${data.headers}');
    print('body: ${data.body}');
    return data;
  }
}

void findAll() async {
  Client client = HttpClientWithInterceptor.build(interceptors: [LoggingInterceptor()]);
  final Response response = await client.get('http://172.18.0.1:8080/transactions');
}
2 respostas

No metodo interceptRequest você não está retornando o data. E tambem falta a palavra reservada "async".

  @override
  Future<RequestData> interceptRequest({RequestData data})async {
    print('Request');
    print('url: ${data.url}');
    print('headers: ${data.headers}');
    print('body: ${data.body}');
    return data;
  }