如何在 Flutter 中过滤 Future 列表?
How to filter Future List in Flutter?
我需要有关筛选列表的帮助。
我在 initState 中填充列表:
Future _organisations;
Future<void> readJson() async {
final String response =
await rootBundle.loadString('assets/organisations.json');
final data = await json.decode(response);
List<Organization> temp = [];
for (var item in data) {
Organization place = Organization(
address: item['Address'],
contactInfo: item['Contact info'],
organization: item['Organization'],
phoneNumber: item['Phone number'],
shortDescription: item['Short description'],
subject: item['Subject'],
tags: item['Tags'],
);
temp.add(place);
}
temp.sort((a, b) => a.organization.compareTo(b.organization));
return temp;
}
@override
void initState() {
super.initState();
_organisations = readJson();
}
然后我在列表上方有一个按钮,我想用它来使用 setState 过滤列表,但是它不起作用。如何过滤 Future 类型的列表?
首先你的方法 readJson()
有一个 return 类型的值 Future<void>
这意味着你将永远无法得到你的 List<Organization>
return编辑
此外,您正在使用异步操作,这意味着您需要使用 await
或 then
来在某些时候获取您的值。
这是一个如何修复代码的示例:
List<Organization> _organisations;
// Simply change the return type to Future<List<Organization>>
Future<List<Organization>> readJson() async {
final String response =
await rootBundle.loadString('assets/organisations.json');
final data = await json.decode(response);
List<Organization> temp = [];
for (var item in data) {
Organization place = Organization(
address: item['Address'],
contactInfo: item['Contact info'],
organization: item['Organization'],
phoneNumber: item['Phone number'],
shortDescription: item['Short description'],
subject: item['Subject'],
tags: item['Tags'],
);
temp.add(place);
}
/// Then you need to compare objects that are comparable.
/// For example if you want to sort your items by their address :
temp.sort((a, b) =>
a.address.toLowerCase().compareTo(b.address.toLowerCase())
);
return temp;
}
@override
void initState() {
super.initState();
/// As you cannot use await in initState you can use then
/// to perform an action once your asynchronous operation is
/// done.
///
/// In this case I am assigning the value to _organisations
/// and causing a rebuild of the interface with setState.
readJson().then((List<Organization> temp) {
setState(() => _organisations = temp);
});
}
Future<void>
没有 return 任何东西。如果你想让它成为 return 列表,它应该是 Future<List>
。当您对列表中的项目进行排序时,您将它们与什么进行比较?项目[“组织”]是什么类型的?它需要是可比较的类型。
我需要有关筛选列表的帮助。
我在 initState 中填充列表:
Future _organisations;
Future<void> readJson() async {
final String response =
await rootBundle.loadString('assets/organisations.json');
final data = await json.decode(response);
List<Organization> temp = [];
for (var item in data) {
Organization place = Organization(
address: item['Address'],
contactInfo: item['Contact info'],
organization: item['Organization'],
phoneNumber: item['Phone number'],
shortDescription: item['Short description'],
subject: item['Subject'],
tags: item['Tags'],
);
temp.add(place);
}
temp.sort((a, b) => a.organization.compareTo(b.organization));
return temp;
}
@override
void initState() {
super.initState();
_organisations = readJson();
}
然后我在列表上方有一个按钮,我想用它来使用 setState 过滤列表,但是它不起作用。如何过滤 Future 类型的列表?
首先你的方法 readJson()
有一个 return 类型的值 Future<void>
这意味着你将永远无法得到你的 List<Organization>
return编辑
此外,您正在使用异步操作,这意味着您需要使用 await
或 then
来在某些时候获取您的值。
这是一个如何修复代码的示例:
List<Organization> _organisations;
// Simply change the return type to Future<List<Organization>>
Future<List<Organization>> readJson() async {
final String response =
await rootBundle.loadString('assets/organisations.json');
final data = await json.decode(response);
List<Organization> temp = [];
for (var item in data) {
Organization place = Organization(
address: item['Address'],
contactInfo: item['Contact info'],
organization: item['Organization'],
phoneNumber: item['Phone number'],
shortDescription: item['Short description'],
subject: item['Subject'],
tags: item['Tags'],
);
temp.add(place);
}
/// Then you need to compare objects that are comparable.
/// For example if you want to sort your items by their address :
temp.sort((a, b) =>
a.address.toLowerCase().compareTo(b.address.toLowerCase())
);
return temp;
}
@override
void initState() {
super.initState();
/// As you cannot use await in initState you can use then
/// to perform an action once your asynchronous operation is
/// done.
///
/// In this case I am assigning the value to _organisations
/// and causing a rebuild of the interface with setState.
readJson().then((List<Organization> temp) {
setState(() => _organisations = temp);
});
}
Future<void>
没有 return 任何东西。如果你想让它成为 return 列表,它应该是 Future<List>
。当您对列表中的项目进行排序时,您将它们与什么进行比较?项目[“组织”]是什么类型的?它需要是可比较的类型。