繁体   English   中英

Flutter,如何在sqflite数据库中进行即时搜索?

[英]Flutter, How to search instantly in sqflite database?

当我搜索任何诗人时,它总是给我第一条记录。 它没有显示我正在搜索的确切记录。

我的任务页面:

TextEditingController _searchInputController = TextEditingController();

变量列表 = [];

var filteredList = [];

布尔 doItJustOnce = 假;

void _filterList(value) {
setState(() {
  filteredList = list
      .where((text) => text.name.toLowerCase().contains(value.toLowerCase())).toList();
});}

我通过其搜索所需记录的 TextField

 TextField(          
    onChanged: (value) {
                    setState(() {
                      // _filterList(value);
                    });
                  },
                ),

未来建设者

 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 数据库中搜索,请帮助我。

您可以使用此sqflite package,这将使您在查询端的任务变得非常容易。 你可以在它的存储库(GitHub)上找到一个例子。 或者你可以试试这个,

Future getSearch async(){try {
  var mapList = await DbHelper.getTaskDetail(
      table: 'CustomerDetails', where: 'name= joe');

  if (mapList != null && mapList.isNotEmpty) {
    yourlist.clear()
    yourlist
        .addAll(mapList.map((e) => yourmodel.fromJson(e)).toList());
  }
}catch(Exception e){
}}

 static Future<List<Map<String, dynamic>>?> getTaskDetail(
  {required String table,
  String? query,
  String? orderBy,
  String? where,
  int? limit}) async {
final db = await instance.taskDatabase;
return db?.query(table, orderBy: orderBy, limit: limit, where: where);}

将结果放入 setState()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM