Flutter mapEventToState只触发一次

Flutter mapEventToState只触发一次,flutter,flutter-dependencies,bloc,Flutter,Flutter Dependencies,Bloc,我在Bloc模式中的状态只更改一次,而mapEventToState不对BlocProvider.of(context.add)(ActiveEvent())做出反应,这是我做错了什么请求 我试图进入Bloc模式,但当我在计数器页面上的切换器中切换状态时,状态会发生变化,之后,它根本不会更新。这就像不要再使用onChangedswitch函数了 我猜问题出在我的流订阅中,它是在CounterBloc承包商中实现的。或者我返回的状态不正确 如果你能向我解释一下错误,我将非常感谢你的帮助 我的集团

我在Bloc模式中的状态只更改一次,而
mapEventToState
不对
BlocProvider.of(context.add)(ActiveEvent())做出反应,这是我做错了什么请求

我试图进入Bloc模式,但当我在计数器页面上的切换器中切换状态时,状态会发生变化,之后,它根本不会更新。这就像不要再使用
onChanged
switch函数了

我猜问题出在我的流订阅中,它是在CounterBloc承包商中实现的。或者我返回的状态不正确

如果你能向我解释一下错误,我将非常感谢你的帮助

我的集团

import 'dart:async';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:practicing_bloc/blocs/counter/counterEvents.dart';
import 'package:practicing_bloc/blocs/counter/counterState.dart';

class CounterBloc extends Bloc<CounterEvent, CounterState> {
  @override
  CounterState get initialState => Active(active: true, count: 0);

  CounterBloc() {
    _counterStream = _counter.stream;
  }

  StreamController<CounterState> _counter = StreamController<CounterState>();
  Stream<CounterState> _counterStream;

  @override
  Stream<CounterState> mapEventToState(CounterEvent event) async* {
    CounterState currentState = state;
    print('currect: $currentState');

    if (event is ActiveEvent) {
      _counter.add(Active(active: true, count: currentState.count));
      yield* _counterStream;
    } else if (event is InactiveEvent) {
      _counter.add(Inactive(active: false, count: currentState.count));
      yield* _counterStream;
    }
  }
}
导入'dart:async';
进口“包装:颤振团/颤振团.飞镖”;
导入“package:practicing_bloc/blocs/counter/counterEvents.dart”;
导入“package:practicing_bloc/blocs/counter/counterState.dart”;
类CounterBloc扩展了Bloc{
@凌驾
CounterState get initialState=>Active(Active:true,count:0);
反集团{
_计数器流=_counter.stream;
}
StreamController _计数器=StreamController();
逆流;
@凌驾
流mapEventToState(计数器事件)异步*{
计数器状态currentState=状态;
打印('current:$currentState');
if(事件为ActiveEvent){
_add(Active(Active:true,count:currentState.count));
收益率*\逆流;
}else if(事件为不活动事件){
_add(非活动(活动:false,计数:currentState.count));
收益率*\逆流;
}
}
}
集团国家

import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';

abstract class CounterState extends Equatable {
  final bool active;
  final int count;

  const CounterState({@required this.active, @required this.count});

  @override
  List<Object> get props => [active, count];

  @override
  String toString() => 'State { active : $active, count : $count }';
}

class Active extends CounterState {
  const Active({@required bool active, @required int count})
      : super(active: active, count: count);
}

class Inactive extends CounterState {
  const Inactive({@required bool active, @required int count})
      : super(active: active, count: count);
}
import'包:equalable/equalable.dart';
导入“package:meta/meta.dart”;
抽象类CounterState扩展了Equatable{
最终bool激活;
最终整数计数;
常量计数器状态({@required this.active,@required this.count});
@凌驾
列表获取道具=>[活动,计数];
@凌驾
字符串toString()=>'状态{active:$active,count:$count}';
}
类活动扩展反状态{
常量活动({@required bool Active,@required int count})
:super(活动:活动,计数:计数);
}
类非活动扩展反状态{
常量不活动({@required bool active,@required int count})
:super(活动:活动,计数:计数);
}
集团活动

import 'package:equatable/equatable.dart';

abstract class CounterEvent extends Equatable {
  const CounterEvent();

  @override
  List<Object> get props => [];
}

class Increase extends CounterEvent {}
class Decrease extends CounterEvent {}
class ActiveEvent extends CounterEvent {}
class InactiveEvent extends CounterEvent {}
import'包:equalable/equalable.dart';
抽象类CounterEvent扩展了Equalable{
常量计数器事件();
@凌驾
列表获取道具=>[];
}
类增加扩展了计数器事件{}
类减少扩展计数器事件{}
类ActiveEvent扩展了计数器事件{}
类InactiveEvent扩展了计数器事件{}
对位

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:practicing_bloc/blocs/counter/counterBloc.dart';

class CounterPage extends StatefulWidget {
  @override
  _CounterPageState createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  bool stateActive = false;

  @override
  Widget build(BuildContext context) {
    //ignore: close_sinks
    dynamic counterBloc = BlocProvider.of<CounterBloc>(context);

    return Scaffold(
      appBar: AppBar(title: Text('Flutter Counter | Page title')),
      body: SafeArea(
        child: BlocBuilder<CounterBloc, CounterState>(
          builder: (context, state) {
            String stateString = state.active ? 'Active' : 'Inactive';

            return Center(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('Counter is : $stateString'),
                  Text('Current counter is : ${state.count}'),
                  Switch(
                    value: stateActive,
                    onChanged: (bool value) {
                      print(counterBloc.state);
                      setState(() {
                        stateActive = value;
                      });
                      CounterEvent newEvent =
                          value ? ActiveEvent() : InactiveEvent();
                      counterBloc.add(newEvent);
                      // print('BloC state: ${counterBloc.state.active} | switch state: ${state.active}');
                    },
                  )
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
进口“包装:颤振团/颤振团.飞镖”;
导入“包:练习_bloc/blocs/counter/counterBloc.dart”;
类CounterPage扩展StatefulWidget{
@凌驾
_CounterPageState createState()=>_CounterPageState();
}
类_CounterPageState扩展状态{
bool stateActive=false;
@凌驾
小部件构建(构建上下文){
//忽略:关闭接收器
dynamiccounterbloc=BlocProvider.of(上下文);
返回脚手架(
appBar:appBar(标题:文本(“颤振计数器|页面标题”),
正文:安全区(
孩子:BlocBuilder(
生成器:(上下文、状态){
String stateString=state.active?'active':'Inactive';
返回中心(
子:列(
crossAxisAlignment:crossAxisAlignment.center,
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
Text('计数器为:$stateString'),
Text('当前计数器为:${state.count}'),
开关(
值:stateActive,
一旦更改:(布尔值){
印刷品(反集团国家);
设置状态(){
stateActive=值;
});
反事件新事件=
值?ActiveEvent():InactiveEvent();
counterBloc.add(newEvent);
//打印('BloC state:${counterBloc.state.active};开关状态:${state.active}');
},
)
],
),
);
},
),
),
);
}
}

基本上不是产生*\u逆流,您需要产生这种状态,即
活动
非活动

改变这个


if(事件为ActiveEvent){
_add(Active(Active:true,count:currentState.count));
收益率*\逆流;
}else if(事件为不活动事件){
_add(非活动(活动:false,计数:currentState.count));
收益率*\逆流;
}
对此

if(事件为ActiveEvent){
不活动的产量(活动:false,计数:currentState.count);
}else if(事件为不活动事件){
有效产量(有效:真,计数:currentState.count);
}

我以为我应该在那里返回一条流。但显然BloCProvider是在引擎盖下做的。。非常感谢你的帮助,Zain!梅格拉德似乎能帮上忙