如何使用StreamBuilder将数据设置为TextEditingController?

时间:2019-05-25 06:05:57

标签: flutter dart stream-builder

嗨,我正在从数据库中加载一些数据(用户先前保存的数据,用于编辑)。我使用StreamBuilder设置数据。 我有用来显示数据的表格,我使用了TextFormField,因为我需要使用验证。 我将TextEditingController连接到TextFormField 然后我将StreamBuilder连接到TextEditingController 但是当我在加载后编辑数据时,它给了我例外。

这是我的表单代码。

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(),
        );
      }

    });
  }

我有这个例外

  

I / flutter(2626):W小部件库引起的异常提示   ╞═════════════════════════════════════════════════ ══════════我/扑   (2626):构建Builder(dirty)时引发了以下断言:   I / flutter(2626):'package:flutter / src / widgets / basic.dart':失败   断言:6220行pos 15:'builder!= null':是I / flutter(2626):   不对。 I / flutter(2626):I / flutter(2626):无论是断言   表示框架本身有错误,或者我们应该提供   基本上是I / flutter(2626):此错误中的更多信息   消息,以帮助您确定和解决根本原因。我/扑   (2626):无论哪种情况,请通过提交错误报告此断言   在GitHub上:I / flutter(2626):
  https://github.com/flutter/flutter/issues/new?template=BUG.md

请任何人帮助我解决此问题。

谢谢!

0 个答案:

没有答案