skip to Main Content

I am a beginner in flutter, I am working on existing code.
I use the intl_phone_field package | Flutter Package to validate a phone number depending on the country.

However, the console shows me this error:

The argument type List<String> can’t be assigned to the parameter type List<Country>?.

I’ll give you an image capture. Please guide me

How do I correct this error? I’ve been struggling for 1 week

3

Answers


  1. Chosen as BEST ANSWER
    IntlPhoneField(
                          countries: countries.map((e) {
                            return e.code != "AM" ? e.code : "";
                          }).toList(),
                          decoration: InputDecoration(
                            labelText: '',
                            border: OutlineInputBorder(
                              borderSide: BorderSide(
                                  color: isDarkMode
                                      ? AppColors.borderDark
                                      : AppColors.borderColor),
                            ),
                            enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(
                                  color: isDarkMode
                                      ? AppColors.borderDark
                                      : AppColors.borderColor),
                            ),
                            focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(
                                  color: isDarkMode
                                      ? AppColors.borderDark
                                      : AppColors.borderColor),
                            ),
                          ),
                          initialCountryCode: 'UZ',
                          onChanged: (phone) {
                            notifier.setEmail(phone.completeNumber);
                          },
                        ),
    

    image capture


  2. Try this
    create this method above the build context

          List<Country> getSelectedCountries() {
        List<String> countryCodes = countries.map((e) {
          return e.code != "AM" ? e.code : "";
        }).toList();
    
        return countries.where((country) {
          return !countryCodes.contains(country.code);
        }).toList();
      }
    

    then call inside the build context like this

    .....
    
      @override
      Widget build(BuildContext context) {
        List<Country> selectedCountries = getSelectedCountries();
        return Scaffold(
          appBar: AppBar(  .....
    

    then use the selectedCountries in countries:

    eg.

     IntlPhoneField(
                    focusNode: focusNode,
                    countries: selectedCountries,
                    decoration: InputDecoration(
                      labelText: 'Phone Number',
                      border: OutlineInputBorder(
                        borderSide: BorderSide(),
                      ),
                    ),
                   ),
    

    Hope this helps!!

    Login or Signup to reply.
  3. The countries property of IntlPhoneField get a list of Country with the possibility for values to be null (List< Country >?), by providing e.code you’re assigning a String instead of a Country.

    I assume that countries is a List of country, instead of e.code you should just put e

    I can see that you want to exclude Armenia which has code "AM" so instead of putting an empty String, you can just put null.

    To fix that, you should replace that part with:

    countries: countries.map((e) {
        return e.code != "AM" ? e : null;
    }).toList(),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search