问题描述
对于显示类别下拉列表中的列表我正在使用Cubit,它的工作原理。我的问题:可以使用Cubit来显示selectedCategory吗?不使用setState吗?现在,当选择类别它不告诉我selectedCategory。 肘:
class CategoriesCubit extends Cubit<CategoriesState> {
final DataBase dataBase;
final Category category;
CategoriesCubit(this.dataBase,this.category)
: super(CategoriesInitial());
StreamSubscription streamSubscription;
Future getCategories() async {
streamSubscription = dataBase.getCategories().listen((data) {
emit(CategoriesLoaded(data));
});
}
void setCategory(String selectedCategory) {
category.categoryID = selectedCategory;
}
}
主要:
class Main extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => CategoriesCubit(DataBase(),Category()),child: DropDownButton(),);
}
}
class DropDownButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
context.bloc<CategoriesCubit>().getCategories();
final category = context.bloc<CategoriesCubit>().category;
return Container(
child: BlocBuilder<CategoriesCubit,CategoriesState>(
builder: (context,state) {
if (state is CategoriesLoaded) {
return DropdownButton(
value: category.categoryID,hint: Text('choose category'),items: state.categories
.map((category) => DropdownMenuItem(
child: Text(category.categoryName),value: category.categoryID,))
.toList(),onChanged: (selectedCategory) {
context.bloc<CategoriesCubit>().setCategory(selectedCategory);
print(selectedCategory);
},);
}
},),);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)