[英]Search through sqflite database (flutter) with a Searchbar
我正在尋找一種通過搜索欄搜索 sqflite 數據庫表的解決方案。 我在我的數據庫中創建了一個“客戶”表,如果我在搜索欄中輸入與數據庫條目相關的名字(我的代碼中的“vorname” - 德語),我希望它顯示在 ListView 中(我想出了如何通過教程將 ListView 作為結果返回)
我有一個 function getCustomers 它返回客戶的所有屬性 - 這可能會有所幫助。
我想到了一個 Databasehelper,它可以將我的數據庫中的表客戶或所有名字 (vorname) 作為一個列表,我可以比較搜索欄中的輸入字符串是否與該列表中的字符串相關。 也可能與 FutureBuilder 相關。 我不知道如何執行此操作,當然我願意接受任何解決方案。 任何幫助將不勝感激 - 如果您需要更多代碼,請告訴我。
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']
);
});
}
我擁有的代碼(來自教程) - 都在同一個文件中,除了顯示搜索欄有效之外沒有任何實際功能:
Function 創建結果:
await Future.delayed(Duration(seconds: 2));
return List.generate(10, (int index) {
return Post(
"Vorname $index",
"Nachname $index",
);
});
}
發布 class:
class Post {
final String vorname;
final String nachname;
Post(this.vorname, this.nachname);
}
搜索欄:
SearchBar(
onSearch: search,
onItemFound: (Post post, int index) {
return ListTile(
title: Text(post.vorname),
subtitle: Text(post.nachname),
);
},
)
你可以試試這個...
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.