- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我一直在关注 - boring flutter show 上的 Flutter 搜索教程.我一直在尝试使用从 future 列表派生的列表来实现相同的功能,其中数据来自 api(在本例中为 Aqueduct 服务器)。
目前我的屏幕列出了 API 中的所有联系人,我现在想针对该联系人列表进行搜索。我假设最好的做法是将相同的列表(已经显示)传递给搜索委托(delegate)。不幸的是,我不确定如何实现这一目标。
我的代码如下(请注意我已经为这个例子精简了一些代码):
class _ContactsScreenState extends State<ContactsScreen> {
static const _usersUrl = //url info;
static final _token = //token info;
static final _headers = {
"Content-type" : "application/json",
"Accept": "application/json",
"Authorization": "Bearer $_token",
};
HttpRequestStatus httpRequestStatus = HttpRequestStatus.NOT_DONE;
Future<List<Contact>> readContacts() async {
final response = await http.get(_usersUrl, headers: _headers);
List responseJson = json.decode(response.body.toString());
List<Contact> contactList = createContactList(responseJson);
return contactList;
}
List<Contact> createContactList(List data) {
List<Contact> list = List();
for (int i = 0; i < data.length; i++) {
String name = data[i]["name"];
int id = data[i]["id"];
Contact user = Contact(name: name, id: id);
list.add(user);
}
return list;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List Contacts'),
actions: [
IconButton(
icon: Icon(Icons.search),
tooltip: 'Search',
onPressed: (){
showSearch(
context: context,
delegate: ContactSearch(),
);
}
),
],
),
body: Container(
child: FutureBuilder<List<Contact>>(
future: readContacts(),
builder: (context, snapshot) {
if (snapshot.hasData) {
//Code which displays the data (works fine);
}
}
),
)
)
}
}
class ContactSearch extends SearchDelegate<Contact> {
@override
<Widget> buildActions(BuildContext context){
//
}
@override
Widget buildLeading(BuildContext context){
//
}
@override
Widget buildSuggestions(BuildContext context){
//Pass contacts list to here and compares agains 'query'
}
@override
Widget buildResults(BuildContext context) {
return container();
}
}
简而言之,我需要通过“ContactSearch()”传递正确的列表/数据:
showSearch(
context: context,
delegate: ContactSearch(),
);
提前致谢。
最佳答案
基本上,您必须将 FutureBuilder 进一步向上移动到小部件层次结构中,因此搜索框和正文都在其下方。然后,您只需将数据推送到 ContactSearch。
例如:
@override
Widget build(BuildContext context) {
return FutureBuilder<List<Contact>>(
future: readContacts(),
builder: (context, snapshot) {
return Scaffold(
appBar: AppBar(
title: Text('List Contacts'),
actions: [
IconButton(
icon: Icon(Icons.search),
tooltip: 'Search',
onPressed: !snapshot.hasData ? null : () {
showSearch(
context: context,
delegate: ContactSearch(snapshot.data),
);
}
),
],
),
body: Container(
child:
(snapshot.hasData ?
//Code which displays the data (works fine);
: /* show errors/progress/etc. */),
),
);
}
);
}
关于dart - Flutter - 将 future 列表传递给 SearchDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54482450/
我一直在尝试自定义 Flutter SearchDelgate到我想要的搜索字段的类型。它有一个名为 appBarTheme 的方法返回类型 ThemeData .通常使用 ThemeData您可以更
我创建了带有标签的产品,因此当用户按下标签按钮时,它将打开 SearchDelegate 并搜索该标签, 那么如何将标签名称传递给搜索委托(delegate)查询? 我的代码: class DataS
我用新类 extends SearchDelegate 创建了一个搜索函数。我想自定义 appBar 背景颜色,字体大小。如何实现? 我的搜索类 import 'package:flutter/mat
我突出显示了这个词,但不是正确的词。 在我的 BuilderSuggection 中,我添加了这样的代码, title: RichText( text: TextSpan(
我正在制作第一个要放在 App Store 上的应用程序。这是一个天气应用程序。将其命名为 Aer,所以请尽快查看!但无论如何,我已经基本完成了。不过有一件事困扰着我。每当我搜索一个城市时(每当我在
我是一个新手,我在将数据传递给我的搜索委托(delegate)类时遇到问题。问题是我有两个标签,我想在事件标签中搜索。所以我试图发送一个变量,告诉它是哪个选项卡以及要查找哪个表的值。 这是我的代码的样
我想通过添加带有一些单选按钮和其他小部件的表单来对搜索结果添加更多过滤器。文档指出 Radio 不保持状态,应该调用父级的 setState onChange 方法来重建小部件。调用 showModa
我的自动完成搜索工作正常,它在列表图 block 中显示结果。但我希望它转到我为每个搜索结果制作的不同页面 例如:这是我的字符串列表 final product=[ "shirts", "Sh
在 SearchDelegate 的当前实现中,没有更改提示文本的选项。当查询为空时,搜索屏幕会在查询字段中显示“搜索” 作为提示文本。 提示文本目前在第 395 行定义如下: final Strin
我想用showSearch从用户那里获取搜索文本(或查询)。我还想将最近的搜索显示为建议,并根据输入的文本过滤搜索历史。 那么我该如何实现呢? 最佳答案 custom_search_delgates.
我一直在关注 - boring flutter show 上的 Flutter 搜索教程.我一直在尝试使用从 future 列表派生的列表来实现相同的功能,其中数据来自 api(在本例中为 Aqued
我使用 SearchDelegate 实现了一个 SearchAppBar Material 并使用来自 this question 的响应更改了搜索委托(delegate)的提示文本默认值. 但是,
我正在尝试显示实时 http json 数据 mtgCardNames使用 buildResults .数据似乎已成功传递(它们被正确打印)但不知何故它们没有显示在屏幕上。为了了解情况,我尝试显示一些
我是一名优秀的程序员,十分优秀!