首页 > 解决方案 > 如何使用 sqflite 通过 searchview 获取结果

问题描述

试图实现 searchview 但没有获取数据或任何它一直在屏幕上显示列表的内容。我有两个数据库文件,即 database_helper 和 blogs_provider,但屏幕上什么也没有发生。作为初学者,我对颤振了解不多。

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: new AppBar(
      centerTitle: true,
      title: appBarTitle,
      toolbarHeight: 80,
      backgroundColor: headerColor,
      actions: <Widget>[
        new IconButton(
          icon: actionIcon,
          onPressed: () {
            setState(() {
              if (this.actionIcon.icon == Icons.search) {
                this.actionIcon = new Icon(Icons.close);
                this.appBarTitle = new TextField(
                  style: new TextStyle(
                    color: Colors.white,
                  ),
                  decoration: new InputDecoration(
                      prefixIcon:
                          new Icon(Icons.search, color: Colors.white),
                      hintText: "Search...",
                      hintStyle: new TextStyle(color: Colors.white)),
                  onChanged: (value) {
                    keyword = value;
                    setState(() {});
                  },
                );

                FutureBuilder(
                  future: Provider.of<BlogsProvider>(context, listen: false).searchBlogs(keyword),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Scaffold(
                        body: Center(
                          child: CircularProgressIndicator(),
                        ),
                      );
                    } else {
                      if (snapshot.connectionState == ConnectionState.done) {
                        return Scaffold(
                          body: Consumer<BlogsProvider>(
                            child: noNotesUI(context),
                            builder: (context, noteprovider, child) =>
                            noteprovider.items.length <= 0
                                ? child
                                : ListView.builder(
                                physics: BouncingScrollPhysics(),
                                itemCount: noteprovider.items.length,
                                itemBuilder: (context, index) {
                                  final item = noteprovider.items[index];
                                  return ListItem(
                                    item.id,
                                    item.title,
                                    item.content,
                                    item.imagePath,
                                    item.date,
                                  );
                                }
                              // },
                            ),
                          ),
                        );
                      }
                    }
                    return Container();
                  },
                );
              } else {
                this.actionIcon = new Icon(Icons.search);
                this.appBarTitle = new Text("Daily Blogs");
              }
            });
          },
        ),
      ]),
  floatingActionButton: FloatingActionButton(
    backgroundColor: headerColor,
    onPressed: () {
      goToNoteEditScreen(context);
    },
    child: Icon(Icons.add),
  ),
  body: FutureBuilder(
    future: Provider.of<BlogsProvider>(context, listen: false).getBlogs(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting) {
        return Scaffold(
          body: Center(
            child: CircularProgressIndicator(),
          ),
        );
      } else {
        if (snapshot.connectionState == ConnectionState.done) {
          return Scaffold(
            body: Consumer<BlogsProvider>(
              child: noNotesUI(context),
              builder: (context, noteprovider, child) =>
                  noteprovider.items.length <= 0
                      ? child
                      : ListView.builder(
                          physics: BouncingScrollPhysics(),
                          itemCount: noteprovider.items.length,
                          itemBuilder: (context, index) {
                            final item = noteprovider.items[index];
                            return ListItem(
                              item.id,
                              item.title,
                              item.content,
                              item.imagePath,
                              item.date,
                            );
                          }
                          // },
                          ),
            ),
          );
        }
      }
      return Container();
    },
  ),
);

}

database_helper.dart

static Future<List<Map<String, dynamic>>> search(String title) async {
final database = await DatabaseHelper.database();

return database.query("blogs", where: "title LIKE ?", whereArgs: ['%$title%']);
}

blogs_provider.dart

Future searchBlogs(String title) async {
final blogsList = await DatabaseHelper.search(title);
_items = blogsList
    .map(
      (item) =>
      BlogModel(
          item['id'], item['title'], item['description'], item['imagePath']),
)
    .toList();
notifyListeners();
}

标签: flutterdartsqflite

解决方案


推荐阅读