【问题标题】:type '_ControllerSubscription<String>' is not a subtype of type 'Stream<dynamic>'类型 '_ControllerSubscription<String>' 不是类型 'Stream<dynamic>' 的子类型
【发布时间】:2020-08-23 08:43:07
【问题描述】:

我正在使用 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();
  }

}

【问题讨论】:

    标签: flutter stream listener bloc


    【解决方案1】:

    尝试替换

    get textStream => _textFieldController.stream
                      .listen( (value){_text = value;});  
    

    get textStream => _textFieldController.stream;
    

    【讨论】:

    • 是的,不会出现错误,但是如何将值存储在_text变量中? (请原谅我..我是流和集团的新手)
    • 在 Bloc 类中你可以这样做 Bloc{ .... var listener; Bloc(){ listener = textStream.listen((val) {_text= val; }) } ... } pastebin.com/cDiSCTE4
    猜你喜欢
    • 2020-07-02
    • 2018-12-14
    • 2021-08-18
    • 2021-06-28
    • 2021-01-14
    • 2021-06-17
    • 2021-10-25
    • 2020-07-03
    • 2019-12-05
    相关资源
    最近更新 更多