【问题标题】:CheckboxListTile has null value using Bloc and Stream on FlutterCheckboxListTile 在 Flutter 上使用 Bloc 和 Stream 具有空值
【发布时间】:2020-10-17 15:15:25
【问题描述】:

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

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

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

【问题讨论】:

    标签: flutter dart bloc


    【解决方案1】:

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

    希望对你有帮助。

    【讨论】:

    • 当我按一次时,它可以工作,但是第二次它失败了'package:flutter/src/material/checkbox_list_tile.dart': Failed assertion: line 269 pos 15: 'value != null': is not true.
    • 我更新了我的答案,请检查您是否从您的validAcceptCondition 收到错误,如果没有,可能问题就在那里。
    猜你喜欢
    • 2021-04-21
    • 2020-06-07
    • 2022-08-21
    • 1970-01-01
    • 2022-01-15
    • 2021-08-30
    • 2021-08-19
    • 2021-09-28
    • 2019-11-20
    相关资源
    最近更新 更多