Type 'Right<SearchCepError, ViaCepInfo>' is not a subtype of type 'String?' in type cast

40 Views Asked by At

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;


}
1

There are 1 best solutions below

0
Md. Yeasin Sheikh On

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

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

More about Either