CheckboxListTile 在 Flutter 上使用 Bloc 和 Stream 具有空值

CheckboxListTile has null value using Bloc and Stream on Flutter

我正在我的 flutter 应用程序(版本 1.17.4)上创建注册表单。我使用 CheckboxListTile 是为了让用户接受这些条款。此小部件由 bloc 和 stream

验证

CheckboxListTile

  Widget _createAcceptConditions(LoginBloc bloc) {
return StreamBuilder(
  stream: bloc.getAcceptCondition,
  builder: (BuildContext context, AsyncSnapshot snapshot) {
    return Container(
      child: Container(
          padding: EdgeInsets.only(left: 5, top: 10),
          child: CheckboxListTile(
              title: Text("I accept the terms"),
              value: bloc.acceptCondition,
              activeColor: Colors.deepPurple,
              controlAffinity: ListTileControlAffinity.leading,
              onChanged: (value) {
                bloc.setAcceptCondition(value);
              })),
    );
  },
);
}

LoginBloc class

  final _acceptCondition = BehaviorSubject<bool>();
  Stream<bool> get getAcceptCondition =>
  _acceptCondition.stream.transform(validAcceptCondition);
//Setter
  Function(bool) get setAcceptCondition => _acceptCondition.sink.add;
//Getter
  bool get acceptCondition => _acceptCondition.value;

这是验证器

final validAcceptCondition =
  StreamTransformer<bool, bool>.fromHandlers(handleData: (accept, sink) {
accept ? sink.add(accept) : sink.addError("You must accept the conditions");
});

当我重新启动应用程序并尝试注册时,我得到了

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building StreamBuilder<bool>(dirty, state: _StreamBuilderBaseState<bool, AsyncSnapshot<bool>>#8f6de):
'package:flutter/src/material/checkbox_list_tile.dart': Failed assertion: line 269 pos 15: 'value != null': is not true.

The relevant error-causing widget was
    StreamBuilder<bool> 
package:neighbour_mobile/…/pages/register_page.dart:205
When the exception was thrown, this was the stack
#2      new CheckboxListTile 
package:flutter/…/material/checkbox_list_tile.dart:269
#3      RegisterPage._createAcceptConditions.<anonymous closure> 
package:neighbour_mobile/…/pages/register_page.dart:211
#4      StreamBuilder.build 
package:flutter/…/widgets/async.dart:509
#5      _StreamBuilderBaseState.build 
package:flutter/…/widgets/async.dart:127
#6      StatefulElement.build 
package:flutter/…/widgets/framework.dart:4619
...

似乎 bloc 正在等待任何用户操作,以便将 truefalse 放入 CheckboxListTile,但是默认值为 null

CheckBox中的值不能为空,当你创建BehaviorSubject Stream时,它们没有任何数据。因此,您可以使用 snapshot value 并在 StreamBuilder 中定义一个 initialData property 以在创建 Stream 时初始化默认值,尝试下一个:

Widget _createAcceptConditions(LoginBloc bloc) {
    return StreamBuilder(
      stream: bloc.getAcceptCondition,
      // Add a initialData 
      initialData: false,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
         // In this point you can validate the snapshot to show the error you are getting
      
         /**if(snapshot.hasError){
           // Do or show something, for example a Snackbar
         }*/
        return Container(
          child: Container(
            padding: EdgeInsets.only(left: 5, top: 10),
            child: CheckboxListTile(
              title: Text("I accept the terms"),
              // You don't need to use the acceptCondition in this section, because you have the value in the snapshot
              // value: bloc.acceptCondition,
              // In this part the first time the snapshot will be false
              value: snapshot.hasData && snapshot.data ? true : false,
              activeColor: Colors.deepPurple,
              controlAffinity: ListTileControlAffinity.leading,
              onChanged: (value) {
                bloc.setAcceptCondition(value);
              },
            ),
          ),
        );
      },
    );
  }

希望对您有所帮助。