首页 > 解决方案 > 在颤振中,有没有办法以编程方式在 CountryPickerDropdown 中设置初始值?

问题描述

我正在尝试以编程方式在 countrypickerdropdown 中设置 initialValue,但无法这样做,而是设置了 initValue ('us') 而不是我的编程值。片段如下

child: CountryPickerDropdown(
                          initialValue: countryCode?? 'us',
                          itemBuilder: _buildDropdownItem,
                          onValuePicked: (Country country) {
                            setState(() {
                              countryName = country.name;
                              print("${country.name}");
                              intlPhoneCode = country.phoneCode;
                              print("${intlPhoneCode}");
                              countryCode = country.isoCode;
                              phoneIsVerified = false;
                            });
                          }

countryCode 值是从 api 调用中获取的,如下所示:

Future<void> getProfile(String id) async {

    try{
      profile = await apiService.getProfile(id);

      countryCode = profile['countryCode'];
    }catch(ex){ 
         ...
    }
}

我什至不能在 initState 中做到这一点。

标签: flutterdart

解决方案


推荐阅读