[英]Search through sqflite database (flutter) with a Searchbar
I am looking for a solution to search through a sqflite database-table via a searchbar.我正在寻找一种通过搜索栏搜索 sqflite 数据库表的解决方案。 I created a "customer" table in my database and if I enter a firstname ("vorname" in my code - german) that correlates with a database entry into the searchbar, I would like that it gets displayed in a ListView (I figured out how to return a ListView as results through tutorials)我在我的数据库中创建了一个“客户”表,如果我在搜索栏中输入与数据库条目相关的名字(我的代码中的“vorname” - 德语),我希望它显示在 ListView 中(我想出了如何通过教程将 ListView 作为结果返回)
I have a function getCustomers which returns all properties of a customer - that might be helpful.我有一个 function getCustomers 它返回客户的所有属性 - 这可能会有所帮助。
I thought of something whith a Databasehelper that gets the table customers in my Database or all firstnames (vorname) as a list and I can compare if the input string in the searchbar correlates with a String in that list.我想到了一个 Databasehelper,它可以将我的数据库中的表客户或所有名字 (vorname) 作为一个列表,我可以比较搜索栏中的输入字符串是否与该列表中的字符串相关。 Probably something with a FutureBuilder too.也可能与 FutureBuilder 相关。 I do not know how to execute this, of course I am open to any solution.我不知道如何执行此操作,当然我愿意接受任何解决方案。 Any help would be appreciated - if you need more code let me know please.任何帮助将不胜感激 - 如果您需要更多代码,请告诉我。
getCustomers() in the file where I also create the database: getCustomers() 在我还创建数据库的文件中:
Future<List<Customer>> getCustomers() async {
Database _db = await database();
List<Map<String, dynamic>> customerMap = await _db.query('customers');
return List.generate(customerMap.length, (index){
return Customer(
id: customerMap[index]['id'],
vorname: customerMap[index]['vorname'],
nachname: customerMap[index]['nachname'],
telefonnummer: customerMap[index]['telefonnummer'],
email: customerMap[index]['email']
);
});
}
The code I have (from tutorial) - all in the same file, that has no real functionality other than showing that searchbar works:我拥有的代码(来自教程) - 都在同一个文件中,除了显示搜索栏有效之外没有任何实际功能:
Function that creates outcome: Function 创建结果:
await Future.delayed(Duration(seconds: 2));
return List.generate(10, (int index) {
return Post(
"Vorname $index",
"Nachname $index",
);
});
}
Post class:发布 class:
class Post {
final String vorname;
final String nachname;
Post(this.vorname, this.nachname);
}
Search-Bar:搜索栏:
SearchBar(
onSearch: search,
onItemFound: (Post post, int index) {
return ListTile(
title: Text(post.vorname),
subtitle: Text(post.nachname),
);
},
)
You can try this...你可以试试这个...
Future<List<Customer>> searchCustomer(String name) async{
var res = await db.query(
"customers",
where: "vorname LIKE ?"
whereArgs: ['%$name']
);
List<Customer> customerList = res.map((element)=>Customer(
id: element[index]['id'],
vorname: element['vorname'],
nachname: element['nachname'],
telefonnummer: element['telefonnummer'],
email: element['email']
));
return customerList;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.