0

I'm trying to get the String 'bairro' from this part of code:

var infoCepJSON = await viaCepSearchCep.searchInfoByCep(cep: '$_cep')

But, its return a Type Either<SearchCepError, ViaCepInfo>. How can I get the string bairro in this case?

If I put: _bairro = infoCepJSON.bairro , its wrong because infoCepJSON is not a String but a object Type Either<SearchCepError, ViaCepInfo> I think.

_consultaCep() async {    
  String _cep;


  String _endereco;
  var viaCepInfo = ViaCepInfo();
  var _bairro = bairroController.text;
  final viaCepSearchCep = ViaCepSearchCep();
  _cep = cepController.text;

  var infoCepJSON = await viaCepSearchCep.searchInfoByCep(cep: '$_cep');
  print(infoCepJSON);


  _bairro = infoCepJSON.bairro;
  bairroController.text = _bairro;


}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56

1 Answers1

0

On either datatype, you can fold the result. In your case

infoCepJSON.fold(
  (failure) {},
  (data) {
    // you can get data from here
    
  },
);

More about Either

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56