flutter - 如何使用 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();
}
解决方案
推荐阅读
- ruby-on-rails - 如何在 ruby on rails 中连接到普遍数据库?
- python - 搜索带有换行符的文本的子字符串
- flask - Flask OS 环境变量 - 更改每个视图调用?
- javascript - 如何使用 React Hook 触发两个组件之间的函数?
- vba - 来自 Microsoft 网站的“Shape.ConnectedShapes 方法(Visio)”示例给出“无效的工作表标识符”错误
- html - 如果该页面中有另一个用户,则在 Django admin 中锁定网页
- python - 有没有办法阻止 scikit 学习在文档术语矩阵中截断我的(大)输出数据?
- linux - eBPF 可以调用动态库吗?
- non-linear-regression - excel中具有多个变量的多项式回归
- python - Nginx 将 websocket 请求传递给 Gunicorn 而不是 Daphne