一尘不染

mapEventToState仅触发一次

flutter

我在做错什么,我的Bloc模式中的状态仅更改一次mapEventToState却不响应BlocProvider.of<CounterBloc>(context).add(ActiveEvent());请求?

我正在尝试使用Bloc模式,但是当我在计数器页面上的切换器中切换状态时,状态会更改,此后它根本不会更新。就像别无选择地onChanged切换功能。

我猜问题出在我的流订阅中,该订阅是在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;
    }
  }
}

集团状态

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 '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 {}

counterPage

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

阅读 497

收藏
2020-08-13

共1个答案

一尘不染

基本上,而不是屈服* _counterStream你需要得到这个IE中的状态ActiveInactive

改变这个

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

对此

    if (event is ActiveEvent) {
      yield Inactive(active: false, count: currentState.count);
    } else if (event is InactiveEvent) {
      yield Active(active: true, count: currentState.count);
    }
2020-08-13