flutter - 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
...
似乎该集团正在等待任何用户操作以便 puttrue
或false
into CheckboxListTile
,但默认值为 null
解决方案
中的值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);
},
),
),
);
},
);
}
希望能帮助到你。
推荐阅读
- javascript - 使用 promise.all 组合两个 Promise
- r - Dplyr,非标准评估和海象算子和卷曲
- java - 为什么 Java 不允许扩展类型参数?
- c# - C# Bouncy Castle:AES CTR 为什么它不是自动递增的
- powerbi - 如何在 Power Query 中实现 do while 循环并读取动态更新的表的最后一行
- c# - 为什么我的函数不能访问它所在类的私有变量 INSIDE?
- python-3.x - Pandas:如何从多索引数据框中获取特定列
- matlab - Matlab 错误 - 'table' 类型的输入参数的未定义函数 'plus'
- angular-material - 如何在Angular Material中并排放置卡片?
- python - 在 lambda 表达式 python 中插入变量值