Flutter 如何使用StreamBuilder将数据设置为TextEditingController?
我正在从数据库加载一些数据(用户以前保存的数据,用于编辑)。我使用了Flutter 如何使用StreamBuilder将数据设置为TextEditingController?,flutter,dart,stream-builder,Flutter,Dart,Stream Builder,我正在从数据库加载一些数据(用户以前保存的数据,用于编辑)。我使用了StreamBuilder来设置数据。 我有一个表单,用来显示数据,我使用TextFormField,因为我需要使用验证。 我已将TextEditingController连接到TextFormField 然后我将StreamBuilder连接到textededitingcontroller 但当我在加载后编辑数据时,它会给我异常 这是我的表格代码 Widget _formUI() { return StreamBuil
StreamBuilder
来设置数据。我有一个表单,用来显示数据,我使用
TextFormField
,因为我需要使用验证。
我已将TextEditingController
连接到TextFormField
然后我将StreamBuilder
连接到textededitingcontroller
但当我在加载后编辑数据时,它会给我异常
这是我的表格代码
Widget _formUI() {
return StreamBuilder<ConcreteEstimationForm>(
stream: _bloc.inputObservable(),
builder: (context, AsyncSnapshot<ConcreteEstimationForm> snapshot) {
if (snapshot.hasData) {
ConcreteEstimationForm form = snapshot.data;
_descriptionController.value = _descriptionController.value.copyWith(text: form.description) ;
_lengthController.text =
form.length == 0 ? "" : form.length.toString();
_widthController.text =
form.width == 0 ? "" : form.width.toString();
_thickController.text =
form.thick == 0 ? "" : form.thick.toString();
_selectedRatio = ratioArr.indexOf(form.mixRatio);
return Column(
children: <Widget>[
TextFormField(
decoration: const InputDecoration(labelText: "Description"),
keyboardType: TextInputType.text,
validator: _descriptionValidator,
controller: _descriptionController,
),
Row(
children: <Widget>[
Expanded(
child: TextFormField(
decoration: const InputDecoration(labelText: "Concrete Length"),
keyboardType: TextInputType.number,
validator: _lengthValidator,
controller: _lengthController,
),
),
Expanded(
child: Text(
'm',
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold),
))
],
),
Row(
children: <Widget>[
Expanded(
child: TextFormField(
decoration: const InputDecoration(labelText: "Concrete Width"),
keyboardType: TextInputType.number,
validator: _widthValidator,
controller: _widthController,
),
),
Expanded(
child: Text(
'm',
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold),
),
)
],
),
Row(
children: <Widget>[
Expanded(
child: TextFormField(
decoration: const InputDecoration(labelText: "Concrete Thick"),
keyboardType: TextInputType.number,
validator: _thickValidator,
controller: _thickController,
),
),
Expanded(
child: Text(
'm',
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold),
),
)
],
)
],
);
} else {
return new Center(
child: new CircularProgressIndicator(),
);
}
});
}
能否确保
\u bloc.inputObservable()
不为空?是。当我在函数中添加普通Text()
时,在ConcreteEstimationForm=snapshot.data下
和addform.description
它在Text()中显示描述有吗?
I/flutter ( 2626): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 2626): The following assertion was thrown building Builder(dirty): I/flutter ( 2626): 'package:flutter/src/widgets/basic.dart': Failed assertion: line 6220 pos 15: 'builder != null': is I/flutter ( 2626): not true. I/flutter ( 2626): I/flutter ( 2626): Either the assertion indicates an error in the framework itself, or we should provide substantially I/flutter ( 2626): more information in this error message to help you determine and fix the underlying cause. I/flutter ( 2626): In either case, please report this assertion by filing a bug on GitHub: I/flutter ( 2626): https://github.com/flutter/flutter/issues/new?template=BUG.md