Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Class Model mobx
- import 'package:mobx/mobx.dart';
- import 'package:dio/dio.dart';
- import 'package:mobx_testing/src/models/address.dart';
- part 'via_cep.g.dart';
- class ViaCep = ViaCepBase with _$ViaCep;
- abstract class ViaCepBase with Store {
- @observable
- String cep = "";
- @observable
- AddressModel address = AddressModel.limpo();
- @action
- getData() async {
- if(cep.length != 8)
- return AddressModel.limpo();
- Response response = await Dio().get(url(this.cep));
- address = AddressModel.fromJson(response.data);
- }
- String url(String cep) => 'https://viacep.com.br/ws/$cep/json/';
- }
- //Cód no main.dart
- Column(
- Column(
- mainAxisAlignment: MainAxisAlignment.start,
- children: <Widget>[
- TextField(
- onChanged: (value) {
- viacep.cep = value;
- viacep.getData();
- },
- decoration: InputDecoration(
- hintText: "Digite o seu Cep", border: OutlineInputBorder()),
- ),
- Observer(
- builder: (context) => Text(
- '${json.encode(viacep.address)}',
- style: Theme.of(context).textTheme.display1,
- ),
- ),
- ],
- )
- //Class Address
- import 'dart:convert';
- class AddressModel {
- String cep;
- String logradouro;
- String complemento;
- String bairro;
- String localidade;
- String uf;
- String unidade;
- String ibge;
- String gia;
- AddressModel.limpo() {
- this.cep = "";
- this.logradouro = "";
- this.complemento = "";
- this.bairro = "";
- this.localidade = "";
- this.uf = "";
- this.unidade = "";
- this.ibge = "";
- this.gia = "";
- }
- AddressModel(
- {this.cep,
- this.logradouro,
- this.complemento,
- this.bairro,
- this.localidade,
- this.uf,
- this.unidade,
- this.ibge,
- this.gia});
- AddressModel.fromJson(Map<String, dynamic> json) {
- cep = json['cep'];
- logradouro = json['logradouro'];
- complemento = json['complemento'];
- bairro = json['bairro'];
- localidade = json['localidade'];
- uf = json['uf'];
- unidade = json['unidade'];
- ibge = json['ibge'];
- gia = json['gia'];
- }
- Map<String, dynamic> toJson() {
- final Map<String, dynamic> data = new Map<String, dynamic>();
- data['cep'] = this.cep;
- data['logradouro'] = this.logradouro;
- data['complemento'] = this.complemento;
- data['bairro'] = this.bairro;
- data['localidade'] = this.localidade;
- data['uf'] = this.uf;
- data['unidade'] = this.unidade;
- data['ibge'] = this.ibge;
- data['gia'] = this.gia;
- return data;
- }
- @override
- String toString() => json.encode(toJson());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement