0

I want to create a new address model. Doing so, beforehand I check a data structure if this contains specific information and then extract them and store it into a variable. After all if-clauses I want to use these variables to create a new object of type Address. However, all the in-between stored variables are not recognized:

final List type = c['types'];
          if (type.contains('street_number')) {
            final streetNumber = c['long_name'];
          }
          if (type.contains('route')) {
            final street = c['long_name'];
          }
          if (type.contains('locality')) {
            final city = c['long_name'];
          }
          if (type.contains('postal_code')) {
            final zipCode = c['long_name'];
          }
          final address = Address(
              country: null,
              postalCode: zipCode,   //Undefinex name 'zipCode'
              city: city,
              streetNumber: streetNumber,
              long: null,
              lat: null);
        });

1 Answer 1

1

Variables which are declared in a code block will be removed after that block. So you have to declare that Variables before those blocks:

dynamic streetNumber;
if (type.contains('street_number')) {
   streetNumber = c['long_name'];
}
dynamic street;
if (type.contains('route')) {
    street = c['long_name'];
}
dynamic city;
if (type.contains('locality')) {
  city = c['long_name'];
}
dynamic zipCode;
if (type.contains('postal_code')) {
  zipCode = c['long_name'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.