├── .idea ├── .name ├── encodings.xml ├── misc.xml ├── vcs.xml ├── modules.xml ├── via_cep.iml ├── libraries │ ├── Dart_SDK.xml │ └── Dart_Packages.xml └── workspace.xml ├── test └── via_cep_test.dart ├── lib ├── via_cep.dart └── src │ └── via_cep_base.dart ├── .gitignore ├── analysis_options.yaml ├── pubspec.yaml ├── example └── via_cep_example.dart ├── CHANGELOG.md ├── LICENSE └── README.md /.idea/.name: -------------------------------------------------------------------------------- 1 | via_cep -------------------------------------------------------------------------------- /test/via_cep_test.dart: -------------------------------------------------------------------------------- 1 | main() { 2 | 3 | } -------------------------------------------------------------------------------- /lib/via_cep.dart: -------------------------------------------------------------------------------- 1 | library via_cep; 2 | 3 | export 'src/via_cep_base.dart'; -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | # Remove the following pattern if you wish to check in your lock file 5 | pubspec.lock 6 | 7 | # Conventional directory for build outputs 8 | build/ 9 | 10 | # Directory created by dartdoc 11 | doc/api/ 12 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | # For lint rules and documentation, see http://dart-lang.github.io/linter/lints. 7 | # Uncomment to specify additional rules. 8 | # linter: 9 | # rules: 10 | # - camel_case_types 11 | 12 | analyzer: 13 | # exclude: 14 | # - path/to/excluded/files/** 15 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: via_cep 2 | description: Uma biblioteca para consulta de CEP para desenvolvedores Dart (Console, Web e Flutter). 3 | version: 1.0.6 4 | homepage: https://viacep.com.br/ 5 | repository: https://github.com/mateuspenha/via_cep 6 | issue_tracker: https://github.com/mateuspenha/via_cep/issues 7 | author: Mateus Penha 8 | 9 | environment: 10 | sdk: '>=2.1.0 <3.0.0' 11 | 12 | dependencies: 13 | http: ^0.12.0+1 14 | xml2json: ^3.0.3 15 | 16 | dev_dependencies: 17 | pedantic: ^1.0.0 18 | test: ^1.0.0 19 | -------------------------------------------------------------------------------- /example/via_cep_example.dart: -------------------------------------------------------------------------------- 1 | import 'package:via_cep/via_cep.dart'; 2 | 3 | main() async { 4 | var CEP = new via_cep(); 5 | 6 | var result = await CEP.searchCEP('12512170', 'json', ''); 7 | 8 | // Sucesso 9 | if (CEP.getResponse() == 200) { 10 | print('CEP: '+CEP.getCEP()); 11 | print('Logradouro: '+CEP.getLogradouro()); 12 | print('Complemento: '+CEP.getComplemento()); 13 | print('Bairro: '+CEP.getBairro()); 14 | print('Localidade: '+CEP.getLocalidade()); 15 | print('UF: '+CEP.getUF()); 16 | print('Unidade: '+CEP.getUnidade()); 17 | print('IBGE '+CEP.getIBGE()); 18 | print('GIA: '+CEP.getGIA()); 19 | } else { 20 | print('Código de Retorno: '+CEP.getResponse().toString()); 21 | print('Erro: '+CEP.getBody()); 22 | } 23 | } -------------------------------------------------------------------------------- /.idea/via_cep.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.0.6 2 | 3 | - Correção do arquivo example/via_cep_example.dart. 4 | - Correção do arquivo test/via_cep_test.dart. 5 | 6 | ## 1.0.5 7 | 8 | - Correção do CHANGELOG.md. 9 | 10 | ## 1.0.4 11 | 12 | - Correção e melhoria do README.md. 13 | - Correção e melhoria do arquivo de exemplo de uso. 14 | 15 | ## 1.0.3 16 | 17 | - Correção da URL da API. 18 | - Criação do parâmetro para possibilitar modificação do case sensitive de resposta (null, 'lower', 'case'). 19 | 20 | ## 1.0.2 21 | 22 | - Correção dos caminhos do github. 23 | - Execução do dartfmt como sugerido. 24 | 25 | ## 1.0.1 26 | 27 | - Correção de sugestões do Google ao Package (quantidade de caracteres da descrição do pacote e homepage). 28 | - Declaração de import que faltava no arquivo via_cep_base. 29 | 30 | ## 1.0.0 31 | 32 | - Versão inicial, desenvolvido por Mateus Penha. -------------------------------------------------------------------------------- /.idea/libraries/Dart_SDK.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2014, the Dart project authors. All rights reserved. 2 | Redistribution and use in source and binary forms, with or without 3 | modification, are permitted provided that the following conditions are 4 | met: 5 | 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above 9 | copyright notice, this list of conditions and the following 10 | disclaimer in the documentation and/or other materials provided 11 | with the distribution. 12 | * Neither the name of Google Inc. nor the names of its 13 | contributors may be used to endorse or promote products derived 14 | from this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Package para integração com API ViaCEP (https://www.viacep.com.br). Se você estava procurando um webservice gratuito e de alto desempenho para consultar Códigos de Endereçamento Postal (CEP) do Brasil? Utilize o serviço, melhore a qualidade de suas aplicações e colabore para manter esta base de dados atualizada. 2 | 3 | ## Uso 4 | 5 | Um simples exemplo de uso: 6 | 7 | ```dart 8 | import 'package:via_cep/via_cep.dart'; 9 | 10 | main() async { 11 | var CEP = new via_cep(); 12 | 13 | var result = await CEP.searchCEP('12512170', 'json', ''); 14 | 15 | // Sucesso 16 | if (CEP.getResponse() == 200) { 17 | print('CEP: '+CEP.getCEP()); 18 | print('Logradouro: '+CEP.getLogradouro()); 19 | print('Complemento: '+CEP.getComplemento()); 20 | print('Bairro: '+CEP.getBairro()); 21 | print('Localidade: '+CEP.getLocalidade()); 22 | print('UF: '+CEP.getUF()); 23 | print('Unidade: '+CEP.getUnidade()); 24 | print('IBGE '+CEP.getIBGE()); 25 | print('GIA: '+CEP.getGIA()); 26 | // Falha 27 | } else { 28 | print('Código de Retorno: '+CEP.getResponse().toString()); 29 | print('Erro: '+CEP.getBody()); 30 | } 31 | } 32 | ``` 33 | 34 | ## Recursos 35 | 36 | searchCEP - Método que realiza a consulta do CEP. 37 | 38 | Parâmetros: searchCEP(String CEP, output, sensitive) 39 | Srting CEP = CEP (somente números, sem caracteres especiais e com 8 digitos) 40 | 41 | String output = Tipo de saída de dados, para retorno por meio de get() é indiferente ao usuário, porém para retorno do Body haverá diferenciação (recomenda-se sempre usar json). 42 | Opções: json 43 | xml 44 | piped 45 | querty 46 | 47 | String sensitive = Tipo de saída dos dados, normal, lowercase ou upppercase. 48 | Opções: '' (em branco) ou null 49 | lower 50 | uper 51 | 52 | Qualquer formato texto informado não reconhecido será inerpretad como branco ou nulo. 53 | 54 | gets 55 | 56 | getBody(); // Retorna na integralidade a resposta da API. 57 | getResponse(); // Retorna o código da consulta a API (Padrão 200 para sucesso). 58 | 59 | Quando houver sucesso (código 200) os métodos abaixo trazem retorno, do contrário retornam null; 60 | 61 | getCEP(); // Recupera o CEP (formatado). 62 | getLogradouro(); // Recupera o Lograouro (Ex.: Rua... Avenida, Etc.). 63 | getComplemento(); // Recupera o Complemento. 64 | getBairro(); // Recupera o Bairro. 65 | getLocalidade(); // Recupera a Cidade do CEP. 66 | getUF(); // Recupera o Estado (sigla) do CEP. 67 | getUnidade(); // Recupera a Unidade do CEP. 68 | getIBGE(); // Recupera o Código de Município IBGE do CEP. 69 | getGIA(); // Recupera o Código GIA do CEP. 70 | 71 | 72 | Para conhecer melhor o funcionamento acesse o site de origem da API (https://www.viacep.com.br) 73 | 74 | ## Funcionalidades e Bugs 75 | 76 | Por favor para requisições de funcionalidades e bugs [issue tracker][tracker]. 77 | 78 | [tracker]: https://github.com/mateuspenha/via_cep/issues -------------------------------------------------------------------------------- /lib/src/via_cep_base.dart: -------------------------------------------------------------------------------- 1 | import 'package:http/http.dart' as http; 2 | import 'package:xml2json/xml2json.dart' as xml; 3 | import 'dart:convert'; 4 | 5 | class via_cep { 6 | String _URLBase = 'https://viacep.com.br/ws/'; 7 | 8 | String _Body; 9 | 10 | int _Response; 11 | 12 | String _LastCEP; 13 | 14 | String _sensitive; 15 | 16 | // variables return 17 | String _return01, _return02, _return03, _return04, _return05, _return06, _return07, _return08, _return09; 18 | 19 | via_cep() { 20 | clear(); 21 | 22 | _LastCEP = null; 23 | 24 | _Body = null; 25 | 26 | _Response = null; 27 | } 28 | 29 | void clear() { 30 | _return01 = null; 31 | _return02 = null; 32 | _return03 = null; 33 | _return04 = null; 34 | _return05 = null; 35 | _return06 = null; 36 | _return07 = null; 37 | _return08 = null; 38 | _return09 = null; 39 | 40 | _sensitive = null; 41 | } 42 | 43 | Future searchCEP(String CEP, output, sensitive) async { 44 | _LastCEP = CEP; 45 | 46 | _sensitive = sensitive.toString().toLowerCase().trim(); 47 | 48 | String _URLAccess = _URLBase+CEP+'/'+output; 49 | 50 | var response = await http.get(_URLAccess); 51 | 52 | _Response = response.statusCode; 53 | _Body = response.body; 54 | 55 | if (_Response != 200) 56 | clear(); 57 | else { 58 | if (output == 'json') { 59 | Map CEPdata = jsonDecode(_Body); 60 | 61 | _return01 = CEPdata['cep']; 62 | _return02 = CEPdata['logradouro']; 63 | _return03 = CEPdata['complemento']; 64 | _return04 = CEPdata['bairro']; 65 | _return05 = CEPdata['localidade']; 66 | _return06 = CEPdata['uf']; 67 | _return07 = CEPdata['unidade']; 68 | _return08 = CEPdata['ibge']; 69 | _return09 = CEPdata['gia']; 70 | 71 | } else if (output == 'xml') { 72 | xml.Xml2Json myTransformer = xml.Xml2Json(); 73 | 74 | String content = _Body; 75 | 76 | myTransformer.parse(content); 77 | 78 | content = myTransformer.toParker(); 79 | 80 | Map CEPdata = jsonDecode(content)['xmlcep']; 81 | 82 | _return01 = CEPdata['cep']; 83 | _return02 = CEPdata['logradouro']; 84 | _return03 = CEPdata['complemento']; 85 | _return04 = CEPdata['bairro']; 86 | _return05 = CEPdata['localidade']; 87 | _return06 = CEPdata['uf']; 88 | _return07 = CEPdata['unidade']; 89 | _return08 = CEPdata['ibge']; 90 | _return09 = CEPdata['gia']; 91 | 92 | } else if (output == 'piped') { 93 | var text = _Body.split('|'); 94 | 95 | _return01 = text[0].split(':')[1]; 96 | _return02 = text[1].split(':')[1]; 97 | _return03 = text[2].split(':')[1]; 98 | _return04 = text[3].split(':')[1]; 99 | _return05 = text[4].split(':')[1]; 100 | _return06 = text[5].split(':')[1]; 101 | _return07 = text[6].split(':')[1]; 102 | _return08 = text[7].split(':')[1]; 103 | _return09 = text[8].split(':')[1]; 104 | } else if (output == 'querty') { 105 | var text = _Body.replaceAll('+', ' ').split('&'); 106 | 107 | _return01 = text[0].split('=')[1]; 108 | _return02 = text[1].split('=')[1]; 109 | _return03 = text[2].split('=')[1]; 110 | _return04 = text[3].split('=')[1]; 111 | _return05 = text[4].split('=')[1]; 112 | _return06 = text[5].split('=')[1]; 113 | _return07 = text[6].split('=')[1]; 114 | _return08 = text[7].split('=')[1]; 115 | _return09 = text[8].split('=')[1]; 116 | } 117 | 118 | } 119 | 120 | } 121 | 122 | String getBody() { 123 | return _Body; 124 | } 125 | 126 | int getResponse() { 127 | return _Response; 128 | } 129 | 130 | String getLastCEP() { 131 | return _LastCEP; 132 | } 133 | 134 | String getCEP() { 135 | return _return01; 136 | } 137 | 138 | String getLogradouro() { 139 | if (_sensitive != null) { 140 | if (_sensitive == 'lower') { 141 | return _return02.toLowerCase(); 142 | } else if (_sensitive == 'upper') { 143 | return _return02.toUpperCase(); 144 | } else { 145 | return _return02; 146 | } 147 | } else { 148 | return _return02; 149 | } 150 | } 151 | 152 | String getComplemento() { 153 | if (_sensitive != null) { 154 | if (_sensitive == 'lower') { 155 | return _return03.toLowerCase(); 156 | } else if (_sensitive == 'upper') { 157 | return _return03.toUpperCase(); 158 | } else { 159 | return _return03; 160 | } 161 | } else { 162 | return _return03; 163 | } 164 | } 165 | 166 | String getBairro() { 167 | if (_sensitive != null) { 168 | if (_sensitive == 'lower') { 169 | return _return04.toLowerCase(); 170 | } else if (_sensitive == 'upper') { 171 | return _return04.toUpperCase(); 172 | } else { 173 | return _return04; 174 | } 175 | } else { 176 | return _return04; 177 | } 178 | } 179 | 180 | String getLocalidade() { 181 | if (_sensitive != null) { 182 | if (_sensitive == 'lower') { 183 | return _return05.toLowerCase(); 184 | } else if (_sensitive == 'upper') { 185 | return _return05.toUpperCase(); 186 | } else { 187 | return _return05; 188 | } 189 | } else { 190 | return _return05; 191 | } 192 | } 193 | 194 | String getUF() { 195 | if (_sensitive != null) { 196 | if (_sensitive == 'lower') { 197 | return _return06.toLowerCase(); 198 | } else if (_sensitive == 'upper') { 199 | return _return06.toUpperCase(); 200 | } else { 201 | return _return06; 202 | } 203 | } else { 204 | return _return06; 205 | } 206 | } 207 | 208 | String getUnidade() { 209 | if (_sensitive != null) { 210 | if (_sensitive == 'lower') { 211 | return _return07.toLowerCase(); 212 | } else if (_sensitive == 'upper') { 213 | return _return07.toUpperCase(); 214 | } else { 215 | return _return07; 216 | } 217 | } else { 218 | return _return07; 219 | } 220 | } 221 | 222 | String getIBGE() { 223 | if (_sensitive != null) { 224 | if (_sensitive == 'lower') { 225 | return _return08.toLowerCase(); 226 | } else if (_sensitive == 'upper') { 227 | return _return08.toUpperCase(); 228 | } else { 229 | return _return08; 230 | } 231 | } else { 232 | return _return08; 233 | } 234 | } 235 | 236 | String getGIA() { 237 | if (_sensitive != null) { 238 | if (_sensitive == 'lower') { 239 | return _return09.toLowerCase(); 240 | } else if (_sensitive == 'upper') { 241 | return _return09.toUpperCase(); 242 | } else { 243 | return _return09; 244 | } 245 | } else { 246 | return _return09; 247 | } 248 | } 249 | 250 | } -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 14 | 15 | 16 | 17 | 18 | 20 | 21 | 36 | 37 | 38 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 |