如何在Flutter Bloc中更新列表
我无法在Flutter Bloc中更新我的列表 我有一个包含另一个列表的“董事会”列表,我的问题是当我更改清单中的属性的价值时,它不会应用我的更改 即使是董事会的属性也不会更改,并且它的价值也不会“发出”
我的状态代码:
part of 'boards_cubit.dart';
enum BoardsStatus { initial, loading, success, error }
class BoardsState extends Equatable {
final BoardsStatus status;
final BoardScreenType screenType;
final List<Board> todoBoards;
final List<Board> doingBoards;
final List<Board> doneBoards;
final List<Board> archiveBoards;
const BoardsState({
required this.status,
required this.todoBoards,
required this.screenType,
required this.doingBoards,
required this.doneBoards,
required this.archiveBoards,
});
factory BoardsState.initial(BoardScreenType screenType) => BoardsState(
status: BoardsStatus.initial,
todoBoards: [],
screenType: screenType,
doingBoards: [],
doneBoards: [],
archiveBoards: [],
);
@override
List<Object?> get props => [
status,
todoBoards,
screenType,
doingBoards,
doneBoards,
archiveBoards,
];
BoardsState copyWith({
BoardsStatus? status,
List<Board>? todoBoards,
BoardScreenType? screenType,
List<Board>? doingBoards,
List<Board>? doneBoards,
List<Board>? archiveBoards,
}) {
return BoardsState(
status: status ?? this.status,
todoBoards: todoBoards ?? this.todoBoards,
screenType: screenType ?? this.screenType,
doingBoards: doingBoards ?? this.doingBoards,
doneBoards: doneBoards ?? this.doneBoards,
archiveBoards: archiveBoards ?? this.archiveBoards,
);
}
}
我的CUBIT代码:
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:timely/futures/timely/domain/entities/board.dart';
import 'package:timely/futures/timely/domain/repositories/timely_repository.dart';
import 'package:timely/futures/timely/presentation/boards/screens/board_screen.dart';
part 'boards_state.dart';
class BoardsCubit extends Cubit<BoardsState> {
final TimelyRepository repository;
BoardsCubit({required this.repository, required BoardScreenType screenType})
: super(BoardsState.initial(screenType)) {
getBoards();
}
@override
void onChange(Change<BoardsState> change) {
super.onChange(change);
debugPrint(
'change state from ${change.currentState} to ${change.nextState}');
}
Future<void> getBoards() async {
await Future.delayed(const Duration(microseconds: 0));
emit(state.copyWith(status: BoardsStatus.loading));
try {
final res = await repository.getTodoBoard();
final doingBoards = await repository.getDoingBoard();
final doneBoards = await repository.getDoneBoard();
final archiveBoards = await repository.getArchiveBoard();
res.fold(
(l) => emit(state.copyWith(status: BoardsStatus.error)),
(r) => emit(state.copyWith(
status: BoardsStatus.success,
todoBoards: r,
)));
doingBoards.fold(
(l) => emit(state.copyWith(status: BoardsStatus.error)),
(r) => emit(state.copyWith(
status: BoardsStatus.success,
doingBoards: r,
)));
doneBoards.fold(
(l) => emit(state.copyWith(status: BoardsStatus.error)),
(r) => emit(state.copyWith(
status: BoardsStatus.success,
doneBoards: r,
)));
archiveBoards.fold(
(l) => emit(state.copyWith(status: BoardsStatus.error)),
(r) => emit(state.copyWith(
status: BoardsStatus.success,
archiveBoards: r,
)));
} catch (e) {
emit(state.copyWith(status: BoardsStatus.error));
}
}
void updateTask(int index, String boardId) {
List<Board> newBoards = [...state.todoBoards];
for (int i = 0; i < newBoards.length; i++) {
if (newBoards[i].id == boardId) {
newBoards[i].checklist?[index].done = !(newBoards[i].checklist?[index].done??false);
}
}
emit(state.copyWith(todoBoards: newBoards));
}
}
I can't update my list in Flutter BLoC
I have a list of 'Boards' that contain another list and my problem is when I change the value of done's a property in the checklist, It does not apply my changes
Even the board's properties won't change and the value of that is not 'emitted'
My State Code :
part of 'boards_cubit.dart';
enum BoardsStatus { initial, loading, success, error }
class BoardsState extends Equatable {
final BoardsStatus status;
final BoardScreenType screenType;
final List<Board> todoBoards;
final List<Board> doingBoards;
final List<Board> doneBoards;
final List<Board> archiveBoards;
const BoardsState({
required this.status,
required this.todoBoards,
required this.screenType,
required this.doingBoards,
required this.doneBoards,
required this.archiveBoards,
});
factory BoardsState.initial(BoardScreenType screenType) => BoardsState(
status: BoardsStatus.initial,
todoBoards: [],
screenType: screenType,
doingBoards: [],
doneBoards: [],
archiveBoards: [],
);
@override
List<Object?> get props => [
status,
todoBoards,
screenType,
doingBoards,
doneBoards,
archiveBoards,
];
BoardsState copyWith({
BoardsStatus? status,
List<Board>? todoBoards,
BoardScreenType? screenType,
List<Board>? doingBoards,
List<Board>? doneBoards,
List<Board>? archiveBoards,
}) {
return BoardsState(
status: status ?? this.status,
todoBoards: todoBoards ?? this.todoBoards,
screenType: screenType ?? this.screenType,
doingBoards: doingBoards ?? this.doingBoards,
doneBoards: doneBoards ?? this.doneBoards,
archiveBoards: archiveBoards ?? this.archiveBoards,
);
}
}
My Cubit Code :
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:timely/futures/timely/domain/entities/board.dart';
import 'package:timely/futures/timely/domain/repositories/timely_repository.dart';
import 'package:timely/futures/timely/presentation/boards/screens/board_screen.dart';
part 'boards_state.dart';
class BoardsCubit extends Cubit<BoardsState> {
final TimelyRepository repository;
BoardsCubit({required this.repository, required BoardScreenType screenType})
: super(BoardsState.initial(screenType)) {
getBoards();
}
@override
void onChange(Change<BoardsState> change) {
super.onChange(change);
debugPrint(
'change state from ${change.currentState} to ${change.nextState}');
}
Future<void> getBoards() async {
await Future.delayed(const Duration(microseconds: 0));
emit(state.copyWith(status: BoardsStatus.loading));
try {
final res = await repository.getTodoBoard();
final doingBoards = await repository.getDoingBoard();
final doneBoards = await repository.getDoneBoard();
final archiveBoards = await repository.getArchiveBoard();
res.fold(
(l) => emit(state.copyWith(status: BoardsStatus.error)),
(r) => emit(state.copyWith(
status: BoardsStatus.success,
todoBoards: r,
)));
doingBoards.fold(
(l) => emit(state.copyWith(status: BoardsStatus.error)),
(r) => emit(state.copyWith(
status: BoardsStatus.success,
doingBoards: r,
)));
doneBoards.fold(
(l) => emit(state.copyWith(status: BoardsStatus.error)),
(r) => emit(state.copyWith(
status: BoardsStatus.success,
doneBoards: r,
)));
archiveBoards.fold(
(l) => emit(state.copyWith(status: BoardsStatus.error)),
(r) => emit(state.copyWith(
status: BoardsStatus.success,
archiveBoards: r,
)));
} catch (e) {
emit(state.copyWith(status: BoardsStatus.error));
}
}
void updateTask(int index, String boardId) {
List<Board> newBoards = [...state.todoBoards];
for (int i = 0; i < newBoards.length; i++) {
if (newBoards[i].id == boardId) {
newBoards[i].checklist?[index].done = !(newBoards[i].checklist?[index].done??false);
}
}
emit(state.copyWith(todoBoards: newBoards));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论