【发布时间】:2021-11-22 05:22:13
【问题描述】:
当我搜索任何诗人时,它总是给我第一条记录。它没有显示我正在搜索的确切记录。
我的任务页面:
TextEditingController _searchInputController = TextEditingController();
var list = [];
var 过滤列表 = [];
bool doItJustOnce = false;
void _filterList(value) {
setState(() {
filteredList = list
.where((text) => text.name.toLowerCase().contains(value.toLowerCase())).toList();
});}
我通过它搜索所需记录的 TextField
TextField(
onChanged: (value) {
setState(() {
// _filterList(value);
});
},
),
FutureBuilder
FutureBuilder<List<Poets>>(
//we call the method, which is in the folder db file database.dart
future: DatabaseHelper.instance.getPoets(),
builder: (BuildContext context, AsyncSnapshot<List<Poets>> snapshot) {
if (snapshot.hasData) {
if (!doItJustOnce) {
//You should define a bool like (bool doItJustOnce = false;) on your state.
list = snapshot.data!;
filteredList = list;
doItJustOnce = !doItJustOnce; //this line helps to do just once.
}
return ListView.builder(
padding: EdgeInsets.symmetric(horizontal: 0, vertical: 7),
physics: BouncingScrollPhysics(),
reverse: false,
//Count all records
itemCount: snapshot.data!.length,
// itemCount: filteredList.length, // snapshot.data!.length, //filterLis.length,
//all the records that are in the Student table are passed to an item Poet item = snapshot.data [index];
itemBuilder: (BuildContext context, int index) {
Poets item = snapshot.data![index];
//delete one register for id
return Dismissible(
key: UniqueKey(),
background: Container(
color: Colors.red,
child: Padding(
padding: const EdgeInsets.fromLTRB(338, 30, 0, 0),
child: Text(
'Delete',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
),
),
),
onDismissed: (direction) {
setState(() {
DatabaseHelper.instance.remove(item.id);
});
},
//Now we paint the list with all the records, which will have a number, name, phone
child: Card(
elevation: 15.0,
color: Colors.white12,
child: Container(
decoration: BoxDecoration(
// color: Color.fromRGBO(64, 75, 96, .9,),
color: Colors.white70,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
bottomLeft: Radius.circular(20.0),
bottomRight: Radius.circular(20.0),
),
),
child: ListTile(
leading: CircleAvatar(
child: Text(item.id.toString()),
backgroundColor: Color.fromRGBO(
64,
75,
96,
.9,
),
foregroundColor: Colors.white,
),
title: Text(
item.name,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(item.f_name),
trailing: Text(item.serial_no.toString()),
//If we press one of the cards, it takes us to the page to edit, with the data onTap:
//This method is in the file add_editclient.dart
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => AddEditClient(
true,
//Here is the record that we want to edit
poet: item,
)));
},
// onLongPress: () {
// setState(() {
// DatabaseHelper.instance.remove(item.id);
// });
// },
),
),
),
);
},
);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
我的问题是如何在 sqflite 数据库中搜索,请帮助我。
【问题讨论】:
-
您可以使用 query() 或 rawQuery() 方法,这些方法返回 List
-
很多不相关的问题很难回答。你可以编辑和拆分它们吗?
-
是的,我已编辑,现在请尝试帮助我
标签: flutter sqlite dart sqflite