3
respostas

Error: A value of type 'Future<List<Transaction>>

Future<Transaction> findAll() async {
  Client client = InterceptedClient.build(interceptors: [LoggingInterceptor()]);
  final Response response =
      await client.get(Uri.http('192.168.0.30:8080', 'transactions'));
  final List<dynamic> decodeJoson = jsonDecode(response.body);
  final List<Transaction> transactions = List();
  for (Map<String, dynamic>  transactionJson in decodeJoson) {
    final Map<String, dynamic> contactJson =  transactionJson['contact'];
    final Transaction transaction = Transaction(
        transactionJson['value'],
        Contact(
          0,
          contactJson['name'],
          contactJson['accountNumber'],
        ));
    transactions.add(transaction);
  }
  return transactions;
}

Error: A value of type 'Future<List>' can't be assigned to a variable of type 'FutureOr'.

  • 'Future' is from 'dart:async'.

  • 'List' is from 'dart:core'.

  • 'Transaction' is from 'package:my_ease/model/transaction.dart' ('lib/model/transaction.dart'). return transactions;

    Na aula o code do professor tb tem o mesmo erro porem tem o corte do vídeo e volta funcionando

3 respostas

Bom dia Izaias, tudo bom?

Está acontecendo isso, pois seu código está divergente com o do Professor. Tem quer ser

Future<List<Transaction>> findAll() async {

Pois está retornando uma lista de Transações.

Qualquer dúvida, estamos a disposição.

Bom dia Izaias, tudo bom?

Segue o code correto:

import 'dart:convert';

import 'package:bytebank/models/contact.dart';
import 'package:bytebank/models/transaction.dart';
import 'package:http/http.dart' as http;
import 'package:http_interceptor/http_interceptor.dart';

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

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

//AQUI ESTA O SEU PROBLEMA - EESQUECEU DE COLOCAR Future<List<Transaction>>
Future<List<Transaction>> findAll() async {
  http.Client client =
      InterceptedClient.build(interceptors: [LoggingInterceptor()]);

  final http.Response response =
      await client.get(Uri.parse('http://10.0.0.166:8080/transactions'));

  final List<dynamic> decodedJson = jsonDecode(response.body);

  final List<Transaction> transactions = [];

  for (Map<String, dynamic> transactionJson in decodedJson) {
    final Map<String, dynamic> contactJson = transactionJson['contact'];
    final Transaction transaction = Transaction(
      transactionJson['value'],
      Contact(
        0,
        contactJson['name'],
        contactJson['accountNumber'],
      ),
    );
    transactions.add(transaction);
  }
  return transactions;
}

Bom dia,

Exatamente Ari, muito obrigado pelo complemento.

Desde já, agradeço sua iniciativa.