-2
I will leave the code block --(Recovers api from https://viacep.com.br/, data listed on Cod, end of studies and training)
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
String _resultado = "Resultado";
_recuperarCep() async {
//String cep = "01001000";
String url = "https://viacep.com.br/ws/01001000/json/";
http.Response response;
response = await http.get(url);
//Map<String, dynamic> retorno = json.decode( response.body );
Map <String, dynamic> retorno = jsonDecode(response.body);
String logradouro = retorno["Logradouro"];
String complemento = retorno["Complemento"];
String bairro = retorno["Bairro"];
String localidade = retorno["Localidade"];
setState(() {
_resultado = "${logradouro},${complemento},${bairro},${localidade},";
});
print(
"Resposta logradouro: ${logradouro} complemento ${complemento} bairro ${bairro} localidade ${localidade}"
);
//print("Resposta:"+ response.statusCode.toString());
//print("Resposta:"+ response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Consumo de serviço web"),
),
body: Container(
padding: EdgeInsets.all(40),
child: Column(
children: <Widget>[
Text(_resultado),
RaisedButton(
child: Text("Clique aqui"),
onPressed: _recuperarCep,
)
],
),
),
);
}
}
perfect worked out, it was an inattention of mine, I am very grateful for your help vlw thank you.
– Heverson Carlos