【发布时间】:2021-12-21 10:14:27
【问题描述】:
我使用 futureBuilder 在 TextFormFields 中显示日期,如果我在 futureBuilder 中为我在 DateTimePicker 中选择的日期调用的 web 服务中有数据,则 TextFormField 被禁用并在其中显示数据。否则,启用 textFormField。
我还有一个按钮,如果收到数据,我想禁用它,如果没有,我想启用它,所以我使用了一个布尔值。
这是我的代码:
child: FutureBuilder<double?>(
future: getTimes(selectedDate),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData){
_timeController.clear();
setState(() {
_isButtonDisabled = false;
});
return TextFormField(
controller: _timeController,
textAlign: TextAlign.center,
enabled: false,
decoration: InputDecoration(
hintText: snapshot.data.toString() + " h",
contentPadding: EdgeInsets.zero,
filled: true,
fillColor: Colors.white70
),
);
}
else {
setState(() {
_isButtonDisabled = true;
});
return TextFormField(
controller: _timeController,
textAlign: TextAlign.center,
enabled: true,
decoration: InputDecoration(
hintText: "0 h",
contentPadding: EdgeInsets.zero,
filled: true,
fillColor: Colors.white
),
);
}
}
)
这导致我在构建期间调用了错误 setState() 或 markNeedsBuild,因此感谢this topic 的回答,我将 setState 方法封装在 WidgetsBinding.instance.addPostFrameCallback(( _)
这是我的代码现在的样子:
child: FutureBuilder<double?>(
future: getTimes(selectedDate),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData){
_timeController.clear();
WidgetsBinding.instance?.addPostFrameCallback((_){
setState(() {
_isButtonDisabled = false;
});
});
return TextFormField(
controller: _timeController,
textAlign: TextAlign.center,
enabled: false,
decoration: InputDecoration(
hintText: snapshot.data.toString() + " h",
contentPadding: EdgeInsets.zero,
filled: true,
fillColor: Colors.white70
),
);
}
else {
WidgetsBinding.instance?.addPostFrameCallback((_){
setState(() {
_isButtonDisabled = true;
});
});
return TextFormField(
controller: _timeController,
textAlign: TextAlign.center,
enabled: true,
decoration: InputDecoration(
hintText: "0 h",
contentPadding: EdgeInsets.zero,
filled: true,
fillColor: Colors.white
),
);
}
}
)
我现在遇到的问题是我的 TextFormFields 不再可点击,并且按钮始终处于启用状态,可能是误用/误解了 addPostFrameCallback 函数。
感谢您的帮助,
【问题讨论】:
标签: flutter dart future setstate flutter-futurebuilder