我遇到了 Flutter 下拉菜单的问题。当我选择其中一项时,它会引发错误:
Another exception was thrown: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 481 pos 15: 'value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true.
我正在搜索,人们说这个错误是因为所选元素不属于原始列表而产生的,但经过一些调试后我可以看到它确实如此。我找不到此错误的根源,因此我将不胜感激。
这是我的代码
FeedCategory 模型
import 'package:meta/meta.dart';
class FeedCategory {
static final dbId = "id";
static final dbName = "name";
int id;
String name;
FeedCategory({this.id, @required this.name});
FeedCategory.fromMap(Map<String, dynamic> map)
: this(
id: map[dbId],
name: map[dbName],
);
Map<String, dynamic> toMap() {
return {
dbId: id,
dbName: name,
};
}
@override
String toString() {
return 'FeedCategory{id: $id, name: $name}';
}
}
小部件
import 'package:app2date/repository/repository.dart';
import 'package:app2date/model/FeedSource.dart';
import 'package:app2date/model/FeedCategory.dart';
import 'package:app2date/util/ui.dart';
import 'package:flutter/material.dart';
class ManageFeedSource extends StatefulWidget {
ManageFeedSource({Key key, this.feedSource}) : super(key: key);
final FeedSource feedSource;
@override
_ManageFeedSource createState() => new _ManageFeedSource();
}
class _ManageFeedSource extends State<ManageFeedSource> {
FeedCategory _feedCategory;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('New Feed'),
),
body: new FutureBuilder(
future: Repository.get().getFeedCategories(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
List<FeedCategory> categoriesList = snapshot.data;
if (categoriesList != null) {
return new DropdownButton<FeedCategory>(
hint: Text('Choose category...'),
value: _feedCategory,
items: categoriesList.map((FeedCategory category) {
return DropdownMenuItem<FeedCategory>(
value: category,
child: Text(category.name),
);
}).toList(),
onChanged: (FeedCategory category) {
print('Selected: $category');
setState(() {
_feedCategory = category;
});
},
);
} else {
return Container(
decoration: new BoxDecoration(color: Colors.white),
);
}
},
),
);
}
@override
void initState() {
super.initState();
}
}
存储库 getFeedCategories 方法
Future<List<FeedCategory>> getFeedCategories() async {
return await database.getFeedCategories();
}
数据库 getFeedCategories 方法
Future<List<FeedCategory>> getFeedCategories() async {
var dbClient = await db;
var query = "SELECT * FROM $feedCategoryTableName;";
var result = await dbClient.rawQuery(query);
List<FeedCategory> feedCategories = [];
for (Map<String, dynamic> item in result) {
feedCategories.add(new FeedCategory.fromMap(item));
}
return feedCategories;
}
categoriesList 内容和选择的类别(调试器)
最佳答案
我想我已经解决了您的问题。这源于您使用 FutureBuilder 的方式。
我认为是这样的:
Future 完成,列表建立,等等:
obj 123 ==> FeedCategory(Prueba)
, obj 345 ==> FeedCategory(Categories 2)
从下拉列表中选择项目,调用 setState()
您的 _feedCategory 现在等于 obj 123 ==> FeedCategory(Prueba)
小部件已重建。它再次调用 getFeedCategories()
future 完成,建立 list 等,包括以下项目
obj 567 ==> FeedCategory(Prueba)
, obj 789 ==> FeedCategory(Categories 2)
items.where((DropdownMenuItem item) => item.value == value).length == 1
,但长度 == 0 因为 obj 123 ==>未找到 FeedCategory(Prueba)
。您的问题有几个解决方案。一种是添加 equals operator到比较类别和/或 id 的 FeedCategory
类。
另一种方法是将futurebuilder中使用的Future与更改的部分分开-您可以通过将future保留为成员变量(可能在initState中实例化它?)来做到这一点,或者通过使内部部分builder 到它自己的 Stateful 小部件中(在这种情况下,您可以将 ManageFeedSource 设为 StatelessWidget)。我推荐最后一个选项。
如果这不能解决您的问题,请告诉我,但我很确定这就是它正在做的事情的原因。
https://stackoverflow.com/questions/52064607/