Flutter Bloc 7.0.0 状态在迁移到空安全后未更新

时间:2021-05-07 13:00:02

标签: android flutter dart bloc

我一直在尝试从 bloc 更新状态几个小时但没有成功,我什至尝试在 yield 之前检查状态的属性,并且所有属性都已更新,但状态不会。

这是我的代码

集团

  @override
  Stream<HomeState> mapEventToState(
    HomeEvent event,
  ) async* {
    if (event is HomeStarted) {
      yield HomeLoadSuccess(carts: []);
    }
    if (event is HomeCartAdded) {
      var currentState = state as HomeLoadSuccess;
      var carts = addCart(currentState.carts, event.item, event.qty);
      print(carts);
      //output carts working as expected but the state doesnt change/update
      //yield HomeInitial(); uncomment this line works but meh
      yield currentState.copyWith(carts: carts);
    }
  }

活动

abstract class HomeEvent {}

class HomeStarted extends HomeEvent {}

class HomeCartAdded extends HomeEvent {
  final Item item;
  final double qty;
  HomeCartAdded({required this.item, this.qty = 0.0});
}

状态

abstract class HomeState extends Equatable {
  @override
  List<Object> get props => [];
}

class HomeInitial extends HomeState {}

class HomeLoadInProgress extends HomeState {}

class HomeLoadSuccess extends HomeState {
  final List<Cart> carts;

  HomeLoadSuccess({
    required this.carts,
  });

  HomeLoadSuccess copyWith({List<Cart>? carts}) {
    return HomeLoadSuccess(
      carts: carts ?? this.carts,
    );
  }

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

项目类别

class Item extends Equatable {
  int id;
  String name;
  String picture;
  double price;
  Item({
    required this.id,
    required this.name,
    required this.picture,
    required this.price,
  });

  @override
  List<Object> get props => [id, name, picture, price];
}

购物车类

//class item
class Cart extends Equatable {
  Item item;
  double qty;
  double total;
  Cart({
    required this.item,
    required this.qty,
    required this.total,
  });
  @override
  List<Object> get props => [item, qty, total];
}

我的代码有什么问题还是我在这里遗漏了什么? 我可以让这段代码与之前版本的 bloc 一起工作..谢谢

0 个答案:

没有答案