【问题标题】:Flutter, How to search instantly in sqflite database?Flutter,如何在 sqflite 数据库中即时搜索?
【发布时间】: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


【解决方案1】:

您可以使用这个sqflite 包,这将使您的任务在查询方面变得非常容易。你可以在它的存储库(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()。

【讨论】:

  • 对不起!不清楚。将结果放在 setState() 的什么位置?
猜你喜欢
  • 2020-06-29
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
  • 2021-10-12
  • 2020-09-06
  • 2019-06-03
  • 2019-10-10
  • 1970-01-01
相关资源
最近更新 更多