无法从相同的点击动作颤动中获得相同的状态

Unable to get same State from same Click Action Flutter

我在 BlocLibrary 中遇到问题(https://bloclibrary.dev/) 我需要在 BlocListner/BlocBuilder 中接收相同的状态,让我在代码中解释一下: 这是 Bloc(不多解释):

class CounterBloc extends Bloc<CounterEvent, AppState> {
  @override
  AppState get initialState => InititalState();

  @override
  Stream<AppState> mapEventToState(CounterEvent event) async* {
    switch (event) {
      case CounterEvent.getState:
        yield Counter(value: 0); 
        break;
  }

这是状态 CLASS:

import 'package:flutter_bloc/flutter_bloc.dart';
enum StateEvent { getState }

abstract class AppState extends Equatable {
  const AppState();

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


class Counter extends AppState {
  final int count;
  const Counter({@required this.count});
  @override
  List<Object> get props => [count];

  @override
  String toString() => 'Counter { count: $count }';
}

这是我的集团listener/builder:

BlocListener<CounterBloc, AppState>(
    listener: (context, state) {
      if (state is Counter) {

  **Here I needed same state, means if I press getState, it should print 0 everytime**
        print(state. value);

      }
    },
    child: BlocBuilder<CounterBloc, AppState>(
      builder: (context, state) {
        return Center(
          child: Text(
            '${value}',
            style: TextStyle(fontSize: 24.0),
          ),
        );
      },
    ),
  )

更改您的 AppState,您可以阅读有关 Equatable 用法的更多信息here. This post is mostly a duplication of

You should not use Equatable if you want the same state back-to-back to trigger multiple transitions.

abstract class AppState { 
  const AppState();
} 

class Counter extends AppState { 
  final int count; 

  const Counter({@required this.count}); 

  @override
  String toString() => 'Increment { count: $count }'; 
}