0

So im trying to populate custom values into flutter intl phone field

IntlPhoneField(
              readOnly: config.isReadonly,
              focusNode: focusNode,
              initialCountryCode: 'IN',
              countries: allowedCountries,
              initialValue: initialValue,
              style: context.appTextBold.body3,
              dropdownTextStyle: context.appTextBold.body3,
              decoration: const InputDecoration(
                border: InputBorder.none,
                enabledBorder: InputBorder.none,
                focusedBorder: InputBorder.none,
                errorBorder: InputBorder.none,
                focusedErrorBorder: InputBorder.none,
                counterText: '',
                errorStyle: TextStyle(height: 0),
                isDense: true,
                // less vertical space, no horizontal padding so dropdown hugs left edge
                contentPadding: EdgeInsets.symmetric(vertical: 10),
                floatingLabelBehavior: FloatingLabelBehavior.never,
              ),
              dropdownIconPosition:
                  IconPosition.trailing, // keep icon with country code
              disableLengthCheck: true, // ensure no built-in validation errors
              validator: (value) => null,
              showDropdownIcon: !config.isReadonly,
              onChanged: (phone) {
                if (onChanged != null) {
                  onChanged?.call(phone);
                }
              },
            ),

Im passing the values from api into the phone field as a list. but this ignore the custom list im passing from api, but when i manually add it as shown below it's accepting the values.

abstract class AllowedCountryList {
  static const List<Country> allowedCountryList = [
    Country(
      name: "India",
      nameTranslations: {
        "en": "India",
      },
      flag: "🇮🇳",
      code: "IN",
      dialCode: "91",
      minLength: 10,
      maxLength: 10,
    )
  ];
}

Is there something im missing? Is there any reason why api data is getting ignored? I can see the values reaching the phonefield. But this is getting ignored. I also tried with didupdatewidget. How can i solve this. Also intl_phone_field only accept manually hardcoded values?!

1 Answer 1

0

IntlPhoneField only reads the countries list at build time. If you pass in a list from an API asynchronously, the field won’t update automatically, which is why your API data seems ignored. Hardcoded lists work because they exist when the widget first builds.

Solution:

  1. Render the field after the API data is loaded:
if (allowedCountries.isEmpty) {
  return CircularProgressIndicator();
}

return IntlPhoneField(
  countries: allowedCountries,
  initialCountryCode: 'IN',
  ...
);
  1. Or force a rebuild with a Key when the list changes:
IntlPhoneField(
  key: ValueKey(allowedCountries.hashCode),
  countries: allowedCountries,
  initialCountryCode: 'IN',
  ...
);

IntlPhoneField does not support dynamic updates of the countries list after it’s built, so you need to rebuild the widget to apply API-fetched data.

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.