You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

flutter_multi_formatter中CountryDropDown无法通过setState更新初始国家代码问题求助

flutter_multi_formatter中CountryDropDown无法通过setState更新初始国家代码问题求助

各位大佬,我卡了整整12小时了,实在搞不定来求助!

情况是这样的:我自己封装了一个带GlobalKey的CountryDropDownCustom组件,里面的TextFormField完全正常——当我通过countryDropDownCustomKey.currentState?.updateValue(_initialCountryData)调用状态里的更新方法时,TextFormField能跟着刷新显示新的国家数据。

但换成flutter_multi_formatter里的原生CountryDropDown组件时,同样的操作却完全没反应,它就是不会更新初始选中的国家代码!

我把自定义组件的代码贴出来,麻烦帮我看看问题出在哪:

class CountryDropDownCustom extends StatefulWidget {
  CountryDropDownCustom({super.key, this.initialCountryData, required this.onChanged});
  PhoneCountryData? initialCountryData;
  final ValueChanged<PhoneCountryData> onChanged;

  @override
  State<CountryDropDownCustom> createState() => CountryDropDownCustomState();
}

class CountryDropDownCustomState extends State<CountryDropDownCustom> {
  PhoneCountryData? _initialCountryCode;
  late TextEditingController countryCode;

  void updateValue(PhoneCountryData? newValue) {
    setState(() {
      _initialCountryCode = newValue;
      country...
    });
  }
}

(注:上面的代码没贴完,countryCode控制器的部分被截断了,但核心逻辑是通过setState更新_initialCountryCode)

我试过各种方式,包括直接给CountryDropDown传新的initialCountryData然后调用setState,也试过用GlobalKey去操作它的状态,但都没用。有没有人遇到过类似的问题?求指点!

备注:内容来源于stack exchange,提问作者Mirakle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 07:13:01