Bom dia, estar aparecendo o seguinte erro: em print(transaction): This expression has a type of 'void' so its value can't be used.
void main() {
runApp(BytebankApp());
save(Transaction(200.0, Contact(0, 'Gui', 2000))).then((transaction) => print(transaction));
}
codigo webclient:
import 'dart:convert';
import 'package:bytebank/models/contact.dart';
import 'package:bytebank/models/transaction.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:http_interceptor/http_interceptor.dart';
http.Client client =
InterceptedClient.build(interceptors: [LoggingInterceptor()]);
const String baseUrl = 'http://192.168.1.152:8080/transactions';
class LoggingInterceptor implements InterceptorContract {
@override
Future<RequestData> interceptRequest({@required RequestData data}) async {
debugPrint('Request');
debugPrint('url: ${data.url}');
debugPrint('headers: ${data.headers}');
debugPrint('body: ${data.body}');
return data;
}
@override
Future<ResponseData> interceptResponse({@required ResponseData data}) async {
debugPrint('Response');
debugPrint('status code: ${data.statusCode}');
debugPrint('headers: ${data.headers}');
debugPrint('body: ${data.body}');
return data;
}
}
Future<List<Transaction>> findAll() async {
final http.Response response =
await client.get(Uri.parse(baseUrl)).timeout(const Duration(seconds: 5));
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<void> 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);
client.post(Uri(host: baseUrl),
headers: {
'Content-type': 'application/json',
'password': '1000',
},
body: transactionJson);
final Response response = await client.post(Uri(host: 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'],
),
);
}