Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Transação salva, não aparece no Transaction Feed

Transação aparece como salva no log do console exatamente como na aula, mas ao abrir o Transaction Feed não retorna nenhum resultado. Segue o Log do Console

Performing hot restart...
Syncing files to device ASUS A001D...
Restarted application in 2.747ms.
I/flutter (31477): Request
I/flutter (31477): url: http://192.168.0.110:8080/transactions
I/flutter (31477): headers: {Content-type: application/json; charset=utf-8, password: 1000}
I/flutter (31477): body: {"value":200.0,"contact":{"name":"Gui","accountNumber":2000}}
I/flutter (31477): Response
I/flutter (31477): status code: 200
I/flutter (31477): headers: {content-type: application/json;charset=UTF-8, date: Mon, 17 May 2021 13:05:19 GMT, transfer-encoding: chunked}
I/flutter (31477): body: {"id":"c6d50384-488f-4eb6-9fa3-6c874ecd6fc5","value":200.0,"contact":{"name":"Gui","accountNumber":2000},"dateTime":"2021-05-17T10:05:19.529"}
I/flutter (31477): Transaction{value: 200.0, contact: Contact{id: 0, name: Gui, accountNumber: 2000}}

Segue o código do webclient.dart

import 'dart:convert';
import 'package:Bytebankapp/models/contact.dart';
import 'package:Bytebankapp/models/transaction.dart';
import 'package:http/http.dart';
import 'package:http_interceptor/http_interceptor.dart';


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

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

final Client client = HttpClientWithInterceptor.build(
  interceptors: [LoggingInterceptor()],
);

// const String baseUrl = 'http://192.168.0.110:8080/transactions';
final baseUrl = Uri.http('192.168.0.110:8080', 'transactions');

Future<List<Transaction>> findAll() async {
  // final url = Uri.http('192.168.0.110:8080', 'transactions');
  final response = await client.get(baseUrl);
  final List<dynamic> decodedJson = jsonDecode(response.body);
  final List<Transaction> transactions = List();
  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;
}

Future<Transaction> save(Transaction transaction) async {
  final Map<String, dynamic> transactionMap = {
    'value' : transaction.value,
    'contact' : {
      'name' : transaction.contact.name,
      'accountNumber' : transaction.contact.accountNumber,
    }

  };
  final String transactionJson = jsonEncode(transactionMap);

  final Response response = await client.post(baseUrl, headers: {
    'Content-type': 'application/json',
    'password': '1000',
  }, body: transactionJson
  );
  Map<String, dynamic> json = jsonDecode(response.body);
  final Map<String, dynamic> contactJson = json['contact'];
  return Transaction(
    json['value'],
    Contact(
      0,
      contactJson['name'],
      contactJson['accountNumber'],
    ),
  );
  return transaction;
}

Segue o código do main.dart

import 'package:Bytebankapp/http/webclient.dart';
import 'package:Bytebankapp/models/transaction.dart';
import 'package:Bytebankapp/screens/dashboard.dart';
import 'package:flutter/material.dart';
import 'models/contact.dart';

void main() {
  runApp(BytebankApp());
  save(Transaction(200.0, Contact(0, 'Gui', 2000))).then((transaction) => print(transaction));
}

class BytebankApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.green[900],
        accentColor: Colors.blueAccent[700],
        buttonTheme: ButtonThemeData(
          buttonColor: Colors.blueAccent[700],
          textTheme: ButtonTextTheme.primary,
        ),
      ),
      home: Dashboard(),
    );
  }
}

E segue o código do transaction.dart

import 'contact.dart';

class Transaction {
  final double value;
  final Contact contact;

  Transaction(
      this.value,
      this.contact,
      );

  @override
  String toString() {
    return 'Transaction{value: $value, contact: $contact}';
  }

}
2 respostas
solução!

Olá! Eu tive o mesmo problema. Fazia um tempo que eu havia feito as aulas anteriores. Resolvi da seguinte maneira:

  1. Rodei o server.jar da aula 2.
  2. Peguei o IP novamente para passar no baseUrl. Aqui o IP havia mudado e foi esse o motivo de não trazer as transações. Espero que esse caminho te ajude!

Obrigado!