[英]Flutter GetBuilder Dependent DropDownButton - There should be exactly one item with [DropdownButton]'s value even when value has been reset
I have two DropDownButtons, one to show Categories (Fruits & Vegetables), The second one is showing Products (Apple, letucce, etc..) according to the category selected, so it depends from the first.我有两个 DropDownButtons,一个显示类别(水果和蔬菜),第二个根据选择的类别显示产品(Apple、letucce 等),所以它取决于第一个。
When I select the category, product list is updated to show corresponding items.当我选择类别时,产品列表会更新以显示相应的项目。 If I then select a product, it is selected, but if I change the category again
There should be exactly one item with [DropdownButton]'s value: (previous product value)
error shows up over the products dropDown.如果我然后选择一个产品,它会被选中,但如果我再次更改类别
There should be exactly one item with [DropdownButton]'s value: (previous product value)
错误显示在产品下拉列表中。
I'm trying to use Getx GetBuilder widget to update the screen only when necessary.我正在尝试使用 Getx GetBuilder 小部件仅在必要时更新屏幕。
Here's a simplified code from my proyect to replicate the error:这是我的项目中用于复制错误的简化代码:
Main
@override
Widget build(BuildContext context) {
return GetMaterialApp(
initialRoute: 'form',
initialBinding: GeneralBinding(),
routes: {'form' : (context) => FormScreen()},
);
}
Bindings
class GeneralBinding extends Bindings {
@override
void dependencies() {
Get.lazyPut<FormController>(() => FormController(), fenix: true);
}
}
Controller
class FormController extends GetxController {
final DataAsset dataAsset = DataAsset();
List<Category> categoriesList = <Category>[];
List<Product> productsList = <Product>[];
@override
void onInit() {
super.onInit();
updateCategories();
}
Future<void> updateCategories() async {
List<Category> newData = await fillCategories();
categoriesList.assignAll(newData);
update();
}
Future<void> updateProducts(int category) async {
List<Product> newData = await fillProducts(category);
newData = newData.where((e) => e.category == category).toList();
productsList.assignAll(newData);
update();
}
Future<List<Category>> fillCategories() async {
return dataAsset.categories;
//Other case, bring from Database, thats why I need it like Future
}
Future<List<Product>> fillProducts(int category) async {
return dataAsset.products.where((element) => element.category == category ).toList();
//Other case, bring from Database, thats why I need it like Future
}
}
Models
class Category {
Category({required this.id, required this.desc});
int id;
String desc;
factory Category.fromMap(Map<String, dynamic> json) => Category(
id: json["id"],
desc: json["desc"],
);
}
class Product {
Product({required this.category,required this.id,required this.desc});
int category;
int id;
String desc;
factory Product.fromMap(Map<String, dynamic> json) => Product(
category: json["category"],
id: json["id"],
desc: json["desc"],
);
}
class DataAsset {
List<Map<String, dynamic>> jsonCategories = [{'id': 1, 'desc': 'Fruits'}, {'id': 2, 'desc': 'Vegetables'}];
List<Map<String, dynamic>> jsonProducts = [{'category': 1, 'id': 1, 'desc': 'Apple'}, {'category': 1, 'id': 2, 'desc': 'Grape'}, {'category': 1, 'id': 3, 'desc': 'Orange'}, {'category': 2, 'id': 4, 'desc': 'Lettuce'}, {'category': 2, 'id': 5, 'desc': 'Broccoli'}];
List<Category> get categories {
return List<Category>.from(jsonCategories.map((e) => Category.fromMap(e)));
}
List<Product> get products {
return List<Product>.from(jsonProducts.map((e) => Product.fromMap(e)));
}
}
Screen
class FormScreen extends StatefulWidget {
const FormScreen({Key? key}) : super(key: key);
@override
_FormScreenState createState() => _FormScreenState();
}
class _FormScreenState extends State<FormScreen> {
final _formKey = GlobalKey<FormState>();
int? category;
int? product;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child: Form(key: _formKey,
child: Column(
children: [
buildSelectCategory(),
buildSelectProduct(),
buildSubmit()
],
),
),
),
);
}
Widget buildSelectCategory() {
return GetBuilder<FormController>(builder: (_formController) {
print('Rebuilding categories');
List<Category> categories = _formController.categoriesList;
return DropdownButtonFormField<int>(
value: (category == null)? null : category,
isExpanded: true,
decoration: const InputDecoration(labelText: 'Category'),
items: categories.map((option) {
return DropdownMenuItem(
value: (option.id),
child: (Text(option.desc)),
);
}).toList(),
onChanged: (value) {
category = value;
product = null; //reset value of product variable
_formController.updateProducts(category!);
},
validator: (value) => (value == null)? 'Mandatory field' : null,
);
});
}
Widget buildSelectProduct() {
return GetBuilder<FormController>(builder: (_formController) {
print('Rebuilding products');
List<Product> products = _formController.productsList;
return DropdownButtonFormField<int>(
value: (product == null)? null : product,
isExpanded: true,
decoration: const InputDecoration(labelText: 'Product'),
items: products.map((option) {
return DropdownMenuItem(
value: (option.id),
child: Text(option.desc),
);
}).toList(),
onChanged: (value) async {
product = value;
},
validator: (value) => (value == null)? 'Mandatory field' : null,
);
});
}
Widget buildSubmit() {
return ElevatedButton(
child: const Text('SUBMIT'),
onPressed: () {
if (!_formKey.currentState!.validate() ) return;
//Save object whit category and product value
}
);
}
}
Hope someone can help, please, im stuck希望有人能帮忙,拜托,我卡住了
Things I've tried:我尝试过的事情:
without success没有成功
Flutter 2.5.2 stable channel, get: ^4.3.8 package, (No other dependencies added) Flutter 2.5.2 稳定通道,获取:^4.3.8 包,(未添加其他依赖)
I got it working after several try & error.经过几次尝试和错误,我让它工作了。 Many things I learned from this:
我从中学到了很多东西:
You can use an UniqueKey in each field, so it is rebuilt each time its notified for update, like so:您可以在每个字段中使用 UniqueKey,因此每次通知更新时都会重建它,如下所示:
return DropdownButtonFormField<int>(
key: UniqueKey(), // <-- Here
value: (formControllerGral.product == null)? null : formControllerGral.product,
isExpanded: true,
decoration: const InputDecoration(labelText: 'Product'),
items: newItems,
onChanged: (value) {
formControllerGral.product = value;
},
validator: (value) => (value == null)? 'Mandatory field' : null,
);
and it will work but only because you are explicitly telling the widget its not gonna be the same in each rebuilt.它会起作用,但只是因为您明确告诉小部件它在每次重建中都不会相同。
I didn't like that solution, so i kept trying and worked now with normal DropdownButton's at first and notice that the values of the Dropdown weren't showing when onChanged function was called, so I added functions to update the widget itself, but without refreshing the options list (Like a setState) and this did the trick:我不喜欢那个解决方案,所以我一开始一直尝试并使用普通的 DropdownButton 工作,并注意到调用 onChanged 函数时没有显示 Dropdown 的值,所以我添加了函数来更新小部件本身,但没有刷新选项列表(如 setState),这就成功了:
For this second and final solution:对于第二个也是最后一个解决方案:
View看法
Widget buildSelectCategory() {
return GetBuilder<FormController>(id: 'category', builder: (_) {
List<Category> categories = formControllerGral.categoriesList;
return DropdownButtonFormField<int>(
value: (formControllerGral.category == null)? null : formControllerGral.category,
isExpanded: true,
decoration: const InputDecoration(labelText: 'Category'),
items: categories.map((option) {
return DropdownMenuItem(
value: (option.id),
child: (Text(option.desc)),
);
}).toList(),
onChanged: (value) {
formControllerGral.category = value;
formControllerGral.product = null; //reset value of product variable
formControllerGral.updateCategories(fillData: false); //Like a setState for this widget itself
formControllerGral.updateProducts(value!);
},
validator: (value) => (value == null)? 'Mandatory field' : null,
);
});
}
Widget buildSelectProduct() {
return GetBuilder<FormController>(
id: 'product',
builder: (_) {
List<Product> products = formControllerGral.productsList;
List<DropdownMenuItem<int>>? newItems = products.map((option) {
return DropdownMenuItem(
value: (option.id),
child: Text(option.desc),
);
}).toList();
return DropdownButtonFormField<int>(
value: (formControllerGral.product == null)? null : formControllerGral.product,
isExpanded: true,
decoration: const InputDecoration(labelText: 'Product'),
items: newItems,
onChanged: (value) {
formControllerGral.product = value;
formControllerGral.updateProducts(formControllerGral.category!, fillData: false); //Like a setState for this widget itself
},
validator: (value) => (value == null)? 'Mandatory field' : null,
);
},
);
}
Controller控制器
Future<void> updateCategories({bool fillData = true}) async {
if (fillData) {
List<Category> newData = await fillCategories();
categoriesList.assignAll(newData);
}
update(['category']);
}
Future<void> updateProducts(int category, {bool fillData = true}) async {
if (fillData) {
List<Product> newData = await fillProducts(category);
newData = newData.where((e) => e.category == category).toList();
productsList.assignAll(newData);
}
update(['product']);
}
Probably it was a basic issue I should have notice before, but I not gonna forget it now.可能这是我以前应该注意的一个基本问题,但我现在不会忘记它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.