fernan542 / fl_country_code_picker

A Flutter package for showing a modal that contains country dial code. The user can also search for the available codes and select right from the modal.
MIT License
19 stars 34 forks source link

The getter 'flagUri' isn't defined for the type 'CountryCode'. #14

Closed sinhpn92 closed 1 year ago

sinhpn92 commented 1 year ago

Hi,

I have got error The getter 'flagUri' isn't defined for the type 'CountryCode'. when try to use custom flag image. Have any missing here?

Thank you.

fernan542 commented 1 year ago

Hello πŸ‘‹πŸ» You can access the flagUri as a property of a CountryCode object. Example:

    GestureDetector(
    onTap: () async {
        // Show the country code picker when tapped.
        final code = await countryPicker.showPicker(context: context);
        // Null check
        if (code != null)  { 
          print(code);
          // Access flagUri property.
          print(code.flagUri);
        }
    },
  /// Some omitted values.
sinhpn92 commented 1 year ago

Hello πŸ‘‹πŸ» You can access the flagUri as a property of a CountryCode object. Example:

    GestureDetector(
    onTap: () async {
        // Show the country code picker when tapped.
        final code = await countryPicker.showPicker(context: context);
        // Null check
        if (code != null) print(code);
       // Access flagUri property.
      print(code.flagUri);
    },
  /// Some omitted values.

Thank you so much. I got it. I have installed wrong version :d

sinhpn92 commented 1 year ago

Now I got error Null check operator used on a null value. Can you please help? @fernan542 Thank you

fernan542 commented 1 year ago

That error means that the code is null or the user didn't select from the picker. In order to solve that, you need to check if the code is not null before you use it or any of the property.

        // Check if not null first.
        if (code != null) {
          // Here you're confident that code is not null, 
          // you can access its properties.
          print(code.flagUri);
        } 

Another thing, in order to help you more, I suggest you paste your code and I'll check it. ☺️ I edited my answer above because I forgot to add curly braces. Thanks!

sinhpn92 commented 1 year ago

Thank you @fernan542

This is my code to call the picker:

Future<void> _onFlagPressed() async {
    const countryPicker = FlCountryCodePicker();
    final code = await countryPicker.showPicker(context: context);
    if (code == null) return;
    setState(() {
      _countryCode = code;
    });
  }

And the picker dialog showing up, then I get the error:

Screenshot 2023-05-31 at 10 02 46
fernan542 commented 1 year ago

What version are you using? Can you show the full error log at your console?

sinhpn92 commented 1 year ago

@fernan542 This my version: phone_number: ^2.0.0

Error log here:

Screenshot 2023-05-31 at 20 15 30
fernan542 commented 1 year ago

Have you tried adding the localizations in your MaterialApp?

First, import the package and add an alias to prevent other import errors.

import  'package:fl_country_code_picker/fl_country_code_picker.dart' as flc;

Then add the following values at your app's MaterialApp:

MaterialApp(
    title: 'Your App',
    // Supported locales at the moment.
    // Cannot find your locale? Please make a request.
    supportedLocales: flc.supportedLocales.map((e) => Locale(e)),
    localizationsDelegates: const [
        // Package's localization delegate.
        CountryLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
    ],
    // ... some omitted values
);
sinhpn92 commented 1 year ago

ok got it. It's working now. Thank you so much @fernan542

fernan542 commented 1 year ago

You're always welcome. Thank you for using my package!