Advertisement
JonathanDoe

Mobx with viacep

Sep 10th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.74 KB | None | 0 0
  1. //Class Model mobx
  2.  
  3. import 'package:mobx/mobx.dart';
  4. import 'package:dio/dio.dart';
  5. import 'package:mobx_testing/src/models/address.dart';
  6.  
  7. part 'via_cep.g.dart';
  8.  
  9. class ViaCep = ViaCepBase with _$ViaCep;
  10.  
  11. abstract class ViaCepBase with Store {
  12.   @observable
  13.   String cep = "";
  14.   @observable
  15.   AddressModel address = AddressModel.limpo();
  16.  
  17.   @action
  18.   getData() async {
  19.     if(cep.length != 8)
  20.       return AddressModel.limpo();
  21.     Response response = await Dio().get(url(this.cep));
  22.     address = AddressModel.fromJson(response.data);
  23.   }
  24.  
  25.   String url(String cep) => 'https://viacep.com.br/ws/$cep/json/';
  26. }
  27.  
  28. //Cód no main.dart
  29. Column(
  30.         Column(
  31.         mainAxisAlignment: MainAxisAlignment.start,
  32.         children: <Widget>[
  33.           TextField(
  34.             onChanged: (value) {
  35.               viacep.cep = value;
  36.               viacep.getData();
  37.             },
  38.             decoration: InputDecoration(
  39.                 hintText: "Digite o seu Cep", border: OutlineInputBorder()),
  40.           ),
  41.           Observer(
  42.             builder: (context) => Text(
  43.               '${json.encode(viacep.address)}',
  44.               style: Theme.of(context).textTheme.display1,
  45.             ),
  46.           ),
  47.         ],
  48.       )
  49.  
  50. //Class Address
  51. import 'dart:convert';
  52.  
  53. class AddressModel {
  54.   String cep;
  55.   String logradouro;
  56.   String complemento;
  57.   String bairro;
  58.   String localidade;
  59.   String uf;
  60.   String unidade;
  61.   String ibge;
  62.   String gia;
  63.   AddressModel.limpo() {
  64.       this.cep = "";
  65.       this.logradouro = "";
  66.       this.complemento = "";
  67.       this.bairro = "";
  68.       this.localidade = "";
  69.       this.uf = "";
  70.       this.unidade = "";
  71.       this.ibge = "";
  72.       this.gia = "";
  73.       }
  74.   AddressModel(
  75.       {this.cep,
  76.       this.logradouro,
  77.       this.complemento,
  78.       this.bairro,
  79.       this.localidade,
  80.       this.uf,
  81.       this.unidade,
  82.       this.ibge,
  83.       this.gia});
  84.  
  85.   AddressModel.fromJson(Map<String, dynamic> json) {
  86.     cep = json['cep'];
  87.     logradouro = json['logradouro'];
  88.     complemento = json['complemento'];
  89.     bairro = json['bairro'];
  90.     localidade = json['localidade'];
  91.     uf = json['uf'];
  92.     unidade = json['unidade'];
  93.     ibge = json['ibge'];
  94.     gia = json['gia'];
  95.   }
  96.  
  97.   Map<String, dynamic> toJson() {
  98.     final Map<String, dynamic> data = new Map<String, dynamic>();
  99.     data['cep'] = this.cep;
  100.     data['logradouro'] = this.logradouro;
  101.     data['complemento'] = this.complemento;
  102.     data['bairro'] = this.bairro;
  103.     data['localidade'] = this.localidade;
  104.     data['uf'] = this.uf;
  105.     data['unidade'] = this.unidade;
  106.     data['ibge'] = this.ibge;
  107.     data['gia'] = this.gia;
  108.     return data;
  109.   }
  110.  
  111.   @override
  112.   String toString() => json.encode(toJson());
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement