首页 > 解决方案 > CheckboxListTile 在 Flutter 上使用 Bloc 和 Stream 具有空值

问题描述

我正在我的颤振应用程序(版本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);
              })),
    );
  },
);
}

登录块类

  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
...

似乎该集团正在等待任何用户操作以便 puttruefalseinto CheckboxListTile,但默认值为 null

标签: flutterdartbloc

解决方案


中的值CheckBox不能为空,并且当您创建 aBehaviorSubject 或 a时Stream,它们没有任何数据。因此,您可以使用并在snapshot value其中定义一个initialData 属性以在创建StreamBuilder时初始化默认值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);
              },
            ),
          ),
        );
      },
    );
  }

希望能帮助到你。


推荐阅读