如何创建一个包含状态值的列表?
How to create a list with values from state?
在应用程序中,当我点击按钮时,我在字段中写下组的名称并将其添加到数据库中。组的名称也显示在屏幕上。我的问题是每次我创建一个新组时,它都会在屏幕上被覆盖。
我需要在屏幕上将组名显示为列表,以便添加而不是覆盖。但是实现是这样的,在我的状态下没有列表,并且不可能使用地图显示组的名称。如何将标题显示为列表,以免它们被覆盖?
@immutable
abstract class NewGroupState {
const NewGroupState();
@override
List<Object?> get props => [];
}
class NewGroupInitial extends NewGroupState {
const NewGroupInitial();
}
class AddGroupState extends NewGroupState {
const AddGroupState(this.group);
final Group group;
}
body: BlocBuilder<NewGroupBloc, NewGroupState>(
builder: (context, state) {
return ListTile(
title: Text(state is AddGroupState ? state.group.groupName : ''),
);
},
),
我不确定我是否理解你的问题,这就是你想要做的吗?
如果不是,你能重写你的问题吗?
class AddGroupState extends NewGroupState {
const AddGroupState(this.currentGroup, this.previousGroups);
final Group currentCroup;
final List<Group> previousGroups;
}
class NewGroupBloc extends Bloc<NewGroupState> {
Future<void> addGroupToDB() async {
final newGroup = Group();
try {
// add group to DB
await _groupRepository.add(newGroup);
final updatedGroups = [...state.previousGroups, newGroup];
emit(NewGroupState(newGroup, updatedGroups));
} on DBError catch (e) {
emit(NewGroupErrorState());
}
}
}
编辑:这是一个不好的答案,但作者询问了如何做到这一点。
class NewGroupBloc extends Bloc<NewGroupState> {
late Future<void> Function(Group group) onAddGroup;
Future<void> addGroupToDB() async {
final newGroup = Group();
try {
await _groupRepository.add(newGroup);
await onAddGroup(newGroup);
emit(NewGroupState(newGroup));
} on DBError catch (e) {
emit(NewGroupErrorState());
}
}
}
class _GroupPageState extends State<GroupPage> {
var groups = <Group>[];
NewGroupBloc bloc = // don't know how it was initialized
@override
void initState() {
bloc.onAddGroup = (newGroup) {
setState(() {
groups = [...groups, newGroup];
});
}
}
Widget build(BuildContext context) {
// The same as you already have, but using groups declared in the Widget instead of BLoC's state.
}
}
在应用程序中,当我点击按钮时,我在字段中写下组的名称并将其添加到数据库中。组的名称也显示在屏幕上。我的问题是每次我创建一个新组时,它都会在屏幕上被覆盖。 我需要在屏幕上将组名显示为列表,以便添加而不是覆盖。但是实现是这样的,在我的状态下没有列表,并且不可能使用地图显示组的名称。如何将标题显示为列表,以免它们被覆盖?
@immutable
abstract class NewGroupState {
const NewGroupState();
@override
List<Object?> get props => [];
}
class NewGroupInitial extends NewGroupState {
const NewGroupInitial();
}
class AddGroupState extends NewGroupState {
const AddGroupState(this.group);
final Group group;
}
body: BlocBuilder<NewGroupBloc, NewGroupState>(
builder: (context, state) {
return ListTile(
title: Text(state is AddGroupState ? state.group.groupName : ''),
);
},
),
我不确定我是否理解你的问题,这就是你想要做的吗? 如果不是,你能重写你的问题吗?
class AddGroupState extends NewGroupState {
const AddGroupState(this.currentGroup, this.previousGroups);
final Group currentCroup;
final List<Group> previousGroups;
}
class NewGroupBloc extends Bloc<NewGroupState> {
Future<void> addGroupToDB() async {
final newGroup = Group();
try {
// add group to DB
await _groupRepository.add(newGroup);
final updatedGroups = [...state.previousGroups, newGroup];
emit(NewGroupState(newGroup, updatedGroups));
} on DBError catch (e) {
emit(NewGroupErrorState());
}
}
}
编辑:这是一个不好的答案,但作者询问了如何做到这一点。
class NewGroupBloc extends Bloc<NewGroupState> {
late Future<void> Function(Group group) onAddGroup;
Future<void> addGroupToDB() async {
final newGroup = Group();
try {
await _groupRepository.add(newGroup);
await onAddGroup(newGroup);
emit(NewGroupState(newGroup));
} on DBError catch (e) {
emit(NewGroupErrorState());
}
}
}
class _GroupPageState extends State<GroupPage> {
var groups = <Group>[];
NewGroupBloc bloc = // don't know how it was initialized
@override
void initState() {
bloc.onAddGroup = (newGroup) {
setState(() {
groups = [...groups, newGroup];
});
}
}
Widget build(BuildContext context) {
// The same as you already have, but using groups declared in the Widget instead of BLoC's state.
}
}