首页 > 解决方案 > DropdownButtonFormField 没有得到重建

问题描述

我正在尝试以编程方式更新选定的值。

我使用了各种方法,包括 Consumer 等,并确保更新值并在值更改时调用小部件,但是,DropdownButtonFormField 从未使用最新值重建。

目前,我将 DropdownButtonFormField 包装在 StreamBuilder 中,据推测,只要有通过流发送的新事件,它就应该重建。这是我的代码:

宣言

final StreamController<String> _raceStreamController = new StreamController<String>();

下拉按钮表单字段

return  
    StreamBuilder<String>(
      stream: _raceStreamController.stream,
      builder: (BuildContext context, AsyncSnapshot<String> snapshot) {

        return new DropdownButtonFormField<String>(
          value: snapshot.data,
          hint: new Text(hint,
            textAlign: TextAlign.center),
          isExpanded: true,
          items: items.map((String value) {
            return new DropdownMenuItem<String>(
              child: new Text(value),
              value: value
            );
          }).toList(),
          validator: (value) => value == null ? 'field required' : null,
          onChanged: (value) {} // no use for now,
      );
  });

推送数据

onFocusChange: (focus) async {
  if (!focus) {
    try{
      await userBloc.searchUser(controller.text.toUpperCase());
      _raceStreamController.sink.add(userBloc.user.race);
    } catch(e) {
      if (e.toString() == ERROR_UNAUTHORISED)
        navigateToRoot(context);
    }
  }
}

我试图删除尽可能多的冗余代码。

谢谢你。

标签: flutterconsumerstream-builderdropdownbutton

解决方案


在 Flutter 1.17.2 版本中,该DropdownButtonFormField错误已得到修复,因此请务必升级。

Github 问题:https ://github.com/flutter/flutter/issues/56898

在 1.17.2 版本中修复:https ://github.com/flutter/flutter/wiki/Hotfixes-to-the-Stable-Channel#1172-may-28-2020


推荐阅读