这里是块类代码,在这里我添加了一个名为NavItemVisibilityEvent的事件,但当我在另一个类中调用此事件时,ui不会更新此代码中的问题。
class MainScreenBloc extends Bloc<MainScreenEvent, MainScreenState> {
MainScreenBloc() : super( MainScreenInitial(itemVisibility: itemVisibility)) {
on<NavigationLoadedEvent>((event,emit)async{
print('inside loaded Event');
List<bool>? loadedBoolList = await PrefManager.loadBoolList('navBarList');
if(loadedBoolList == null)
{
print('inside loadedBoolList');
emit(MainScreenInitial(itemVisibility: itemVisibility));
emit(MainScreenLoadedState(currentIndex: 0,itemVisibility:itemVisibility,visible: true));
// emit(MainScreenInitial(itemVisibility: itemVisibility));
}else{
print('after pref loaded Event');
print('loadedBoolList $loadedBoolList');
emit(MainScreenInitial(itemVisibility: loadedBoolList));
emit(MainScreenLoadedState(currentIndex: 0,itemVisibility:loadedBoolList,visible: true));
}
});
on<NavigationEvent>((event, emit) async{
final currentState = state as MainScreenLoadedState;
// List<bool> loadedBoolList = await PrefManager.loadBoolList('navBarList');
// print('loadedBoolList $loadedBoolList');
emit(MainScreenLoadedState(currentIndex: event.currentIndex,
itemVisibility: currentState.itemVisibility,visible: true
));
// print('navList $navList');
// if(navList == null)
// {
// navList = await PrefManager.loadBoolList('navBarList')??itemVisibility;
//
// }else{
// navList = await PrefManager.loadBoolList('navBarList')??itemVisibility;
// emit(MainScreenState(currentIndex: event.currentIndex,
// itemVisibility:navList
// ));
// }
});
on<NavigationHideEvent>((event,emit) {
final currentState = state as MainScreenLoadedState;
emit(MainScreenLoadedState(currentIndex: currentState.currentIndex,visible: event.visible,
itemVisibility:currentState.itemVisibility));
});
on<NavItemVisibilityEvent>((event,emit) async {
final currentState = state as MainScreenLoadedState;
print('inside event visibility');
emit(MainScreenLoadedState(currentIndex: currentState.currentIndex, itemVisibility:event.itemVisibility,
visible: true));
print('final event visibility ${event.itemVisibility}');
await PrefManager.saveBoolList(event.itemVisibility!,'navBarList');
});
}
}
字符串
这是州代码
class MainScreenState {
MainScreenState();
}
class MainScreenInitial extends MainScreenState {
List<bool>? itemVisibility;
MainScreenInitial({this.itemVisibility});
}
class MainScreenLoadedState extends MainScreenState{
final int? currentIndex;
final bool? visible;
List<bool>? itemVisibility;
MainScreenLoadedState({this.currentIndex, this.visible,this.itemVisibility});
}
型
这里是另一个块类,我想访问mainbloc类的状态变量,当我在这个调用中调用NavItemVisibilityEvent时,它不会更新当前的ui。
for (var i in state.toggleFavStates) {
switch (i.key) {
case 'Chat':
print('iteration');
final state = mainScreenBloc.state;
print('state $state');
if(state is MainScreenInitial)
{
print('state value ${state.itemVisibility}');
print('itemVisibility');
navList = state.itemVisibility;
print('navList $navList');
}
print('naVlistss: $navList');
navList?[1] = i.value;
context.read<MainScreenBloc>().add
(NavItemVisibilityEvent(
key: i.key,
singleItem: i.value,
itemVisibility:navList));
//do this to that index.
break;
case 'Schedule':
// navList = mainScreenBloc.state.itemVisibility;
navList?[2] = i.value;
context.read<MainScreenBloc>().add
(NavItemVisibilityEvent(
key: i.key,
singleItem: i.value,
itemVisibility:navList));
break;
case 'Profile':
// navList = mainScreenBloc.state.itemVisibility;
navList?[3] = i.value;
context.read<MainScreenBloc>().add
(NavItemVisibilityEvent(
key: i.key,
singleItem: i.value,
itemVisibility:navList));
break;
default:
break;
}
}
型
在上面的屏幕中,我正在调用
context.read<MainScreenBloc>().add
(NavItemVisibilityEvent(
key: i.key,
singleItem: i.value,
itemVisibility:navList));
```
型
但是它并没有更新我的UI状态。这里我也使用代码来访问mainScreenLoaded状态变量值&这是从另一个Bloc屏幕类访问状态变量值的正确方法吗?
final state = mainScreenBloc.state;
print('state $state');
if(state is MainScreenInitial)
{
print('state value ${state.itemVisibility}');
print('itemVisibility');
navList = state.itemVisibility;
print('navList $navList');
}
```
型
1条答案
按热度按时间bq3bfh9z1#
我不确定我从问题中得到了整个上下文,但作为一般的经验法则,对于块,请确保检查这些东西->
1.你已经覆盖了state中的hashcode和equal to运算符,以及state中的自定义模型。我通常使用https://pub.dev/packages/equatable包来简化代码。
1.每当你试图更新状态中的列表项时,首先创建一个新的列表,然后修改它并发出它。如果你想用不同的属性发出相同的状态,也可以创建copyWith方法并使用它。
字符串
1.如果你想让UI在一个新的状态出现时就更新,请确保你使用了BlocBuilder,并在其中编写了代码。
1.确保块的作用域是全局的,即写在你的主应用程序中,或者至少在你推新屏幕时提供给新屏幕。你可以使用
型