flutter - 输入'_ControllerSubscription' 不是类型 'Stream 的子类型'
问题描述
我正在使用 bloc 将数据输入到 textField 并将其存储在变量中。将侦听器添加到流后,我不断收到错误消息:“类型'_ControllerSubscription'不是类型'Stream'的子类型”。
用户界面:
/****/
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
var bloc = Bloc();
return Container(
color: Colors.white,
child: Column(children: <Widget>[
SizedBox(height: size.height *.2 ,),
StreamBuilder( stream: bloc.textStream,
builder: (context, snapshot ){
return Container(
width: size.height * .5,
height: size.height *.2,
child: TextField(
onChanged: bloc.changeText,
textAlign: TextAlign.right,
decoration: InputDecoration(
hintStyle: TextStyle(
fontSize: 14,
color: Colors.black,
),),
/****
*****/
集团:
import 'dart:async';
class Bloc {
var _text='';
final _textFieldController = StreamController<String>();
get textStream => _textFieldController.stream
.listen( (value){_text = value;});
Function(String) get changeText => _textFieldController.sink.add;
void dispose() {
_textFieldController.close();
}
}
解决方案
尝试更换
get textStream => _textFieldController.stream
.listen( (value){_text = value;});
和
get textStream => _textFieldController.stream;
推荐阅读
- laravel - 删除现有的 Where 子句
- html - 强制更改整个背景颜色,忽略 div(HTML、CSS)
- mulesoft - 如何使用 Exchange API 发布自定义策略?
- python - 有没有用`python-attrs`记录属性的好方法?
- python - 是否可以在不先将对象作为文件保存到谷歌驱动器的情况下从谷歌 colab 下载对象作为文件?
- reactjs - 我应该如何使用带有 post axios 的 react hook useState
- hive - 将 hive 分区表从一个集群复制到另一个集群时出现问题
- c++ - 如何在 Linux 中“反转”POSIX 消息队列?
- scala - 如何为要在 from_json 中使用的 json 定义模式来解析值
- react-native - 如何将 Metro bundler 配置为仅包含某些特定文件?