我使用 Flutter bloc 包創建了一個應用程式。我創建了一個名為 learning_dashboard_screen 的螢屏,其中包含從服務器獲取的串列。在同一個螢屏內,我有一個過濾器按鈕,按下后將使用底部作業表小部件顯示過濾器螢屏。

它為要過濾的串列提供了不同的類別。在 learning_dashboard_screen_state 螢屏中,我得到了一個名為 filteroptions 的地圖,其型別為 Map<String, Map<String, dynamic>>。該地圖用于通過將地圖與復選框小部件映射來填充復選框。
part of 'learning_dashboard_screen_bloc.dart';
class LearningDashboardScreenState extends Equatable {
static ScrollController controller = ScrollController();
final int courseLimit = 3;
final List<GPaginatedCoursesData_paginatedCourses_data> courses;
final DataRetrievalStatus initialCoursesStatus;
final Map<String, Map<String, dynamic>> filterOption;
Map<String, Map<String, dynamic>> getFilterOption() {
return filterOption;
}
final int currPage;
final bool hasNextPage;
final DataRetrievalStatus moreCoursesStatus;
LearningDashboardScreenState(
{this.courses = const [],
this.initialCoursesStatus = DataRetrievalStatus.NOT_INITIALIZED,
this.filterOption = const {
"Certificate Provider": {"NVQ": false, "City and Guilds": false},
"Course Language": {"Sinhala": false, "English": false, "Tamil": false},
"Duration": {"6 Months": false, "8 Months": false, "1Year": false},
"Category": {
"Baby Care": false,
"First Aid": false,
"Adult Care": false,
"Mental Care": false,
"Physiotherapy": false,
"Baby First Aid": false,
"Light Housekeeping": false,
"Assist Methods": false
}
},
this.currPage = 0,
this.hasNextPage = false,
this.moreCoursesStatus = DataRetrievalStatus.NOT_INITIALIZED});
@override
List<Object?> get props => [
courses,
filterOption,
initialCoursesStatus,
courseLimit,
currPage,
hasNextPage,
moreCoursesStatus
];
LearningDashboardScreenState copyWith(
{List<GPaginatedCoursesData_paginatedCourses_data>? course,
DataRetrievalStatus? courseStatus,
int? currPage,
bool? hasNextPage,
Map<String, Map<String, dynamic>>? filterOption,
DataRetrievalStatus? moreCourseStatus}) {
return LearningDashboardScreenState(
courses: course ?? this.courses,
filterOption: filterOption ?? this.filterOption,
initialCoursesStatus: courseStatus ?? this.initialCoursesStatus,
currPage: currPage ?? this.currPage,
hasNextPage: hasNextPage ?? this.hasNextPage,
moreCoursesStatus: moreCourseStatus ?? this.moreCoursesStatus);
}
ScrollController getScrollController() {
return controller;
}
}
我想要的是根據 chackbox 值更改 state 中 filteroption 映射的布林值。為此,我所做的是,
1.我在事件類中創建了一個事件,
class CheckBoxValueChangedEvent extends LearningDashboardScreenEvent {
String filterOption;
String filterValue;
bool isChecked;
CheckBoxValueChangedEvent({required this.filterOption,required this.filterValue,required
this.isChecked});
@override
List<Object?> get props => [];
}
我在按下復選框時呼叫了該事件
CheckBox( label: entry.key, onChanged: (isChecked) { context .read<LearningDashboardScreenBloc>() .add(CheckBoxValueChangedEvent(filterOption: widgetName, filterValue: entry.key, isChecked: isChecked)); }, key: GlobalKey<CheckBoxState>(), initValue: entry.value, ),
3.在bloc類中我寫了改變狀態類中bool值并發出狀態的函式,
void _onCheckBoxValueChangedEvent(CheckBoxValueChangedEvent event,
Emitter<LearningDashboardScreenState> emit) {
Map<String, Map<String, dynamic>> filterOption = {};
filterOption=new Map<String, Map<String, dynamic>>.from(state.filterOption);
if (event.isChecked = true) {
filterOption[event.filterOption]![event.filterValue] = true;
} else {
filterOption[event.filterOption]![event.filterValue] = false;
}
emit(state.copyWith(filterOption: filterOption));
}
我的問題是當我勾選一個復選框時,
Error: Unhandled error Unsupported operation: Cannot modify unmodifiable map occurred in
Instance of 'LearningDashboardScreenBloc'.
uj5u.com熱心網友回復:
問題是,當您宣告地圖時,const您無法修改地圖。
在您的建構式中filterOption有一個默認值是const:
LearningDashboardScreenState(
{this.courses = const [],
this.initialCoursesStatus = DataRetrievalStatus.NOT_INITIALIZED,
this.filterOption = const {
"Certificate Provider": {"NVQ": false, "City and Guilds": false},
"Course Language": {"Sinhala": false, "English": false, "Tamil": false},
"Duration": {"6 Months": false, "8 Months": false, "1Year": false},
"Category": {
"Baby Care": false,
"First Aid": false,
"Adult Care": false,
"Mental Care": false,
"Physiotherapy": false,
"Baby First Aid": false,
"Light Housekeeping": false,
"Assist Methods": false
},
},
this.currPage = 0,
this.hasNextPage = false,
this.moreCoursesStatus = DataRetrievalStatus.NOT_INITIALIZED}
);
一個可能的解決方法是在初始化串列中定義映射文字,而不是作為默認引數(這樣它就不需要是常量)。
LearningDashboardScreenState(
{this.courses = const [],
this.initialCoursesStatus = DataRetrievalStatus.NOT_INITIALIZED,
Map<String, Map<String, dynamic>>? filterOption,
this.currPage = 0,
this.hasNextPage = false,
this.moreCoursesStatus = DataRetrievalStatus.NOT_INITIALIZED}
): this.filterOption = filterOption ??
{
"Certificate Provider": {"NVQ": false, "City and Guilds": false},
"Course Language": {
"Sinhala": false,
"English": false,
"Tamil": false
},
"Duration": {
"6 Months": false,
"8 Months": false,
"1Year": false
},
"Category": {
"Baby Care": false,
"First Aid": false,
"Adult Care": false,
"Mental Care": false,
"Physiotherapy": false,
"Baby First Aid": false,
"Light Housekeeping": false,
"Assist Methods": false
}
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382755.html
上一篇:不可滾動時展開的ListView
