如何从Flutter SQFlite中获得特定的行计数?
我使用flutter
sqflite
软件包创建了一个本地数据库,用于存储我的任务管理器应用程序 task 。现在,我想计算任务总数,并特别是完成任务
的计数。
摘要:
我想向用户展示您完成了多少个任务以及在UI中必须执行更多任务。为此,我想要我提到的
特定
class Task implements Comparable {
final int id;
final String taskTitle;
bool isDone;
Task({
required this.id,
required this.taskTitle,
required this.isDone,
});
Task.fromRow(Map<String, Object?> row)
: id = row['id'] as int,
taskTitle = row['taskTitle'] as String,
isDone = (row['isDone'] as int) == 1 ? true : false;
@override
String toString() =>
'Task:: id = $id , Title = $taskTitle , is Done = $isDone';
@override
int compareTo(covariant Task other) => other.id.compareTo(id);
}
计数
class TaskDatabase {
final String dbName;
Database? _db;
List<Task> _tasksList = [];
final _streamController = StreamController<List<Task>>.broadcast();
TaskDatabase({required this.dbName});
Stream<List<Task>> all() =>
_streamController.stream.map((tasks) => tasks..sort());
// opening database function
// closing database function
// fetching database data function
// deleting database data and updating etc...
}
。数据库并使用streamBuilder
从该列表数据渲染UI。
示例数据库函数在数据库功能类中存在(OPEN DB FUNC)
Future<bool> open() async {
if (_db != null) {
return true;
}
final directory = await getApplicationDocumentsDirectory();
final path = '${directory.path}/$dbName';
try {
final db = await openDatabase(path);
_db = db;
//creating the database table using sqflite
const createTable = '''CREATE TABLE IF NOT EXISTS "TABLEOFTASKS" (
"id" INTEGER NOT NULL,
"taskTitle" TEXT,
"isDone" INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY("id" AUTOINCREMENT));''';
await db.execute(createTable);
// read all existing task objects from the db
_tasksList = await _fetchTasks();
_streamController.add(_tasksList);
return true;
} catch (e) {
print('error = $e');
return false;
}
}
I created a local database using flutter
sqflite
package to store tasks
for my task manager app. Now I want to count total number of tasks and specially the count of the done tasks
.
Summary:
I want to show the user to how many tasks you have done and how many tasks you have to do more in the UI. To do that I want that specific counts I mentioned.
Modal class for task
class Task implements Comparable {
final int id;
final String taskTitle;
bool isDone;
Task({
required this.id,
required this.taskTitle,
required this.isDone,
});
Task.fromRow(Map<String, Object?> row)
: id = row['id'] as int,
taskTitle = row['taskTitle'] as String,
isDone = (row['isDone'] as int) == 1 ? true : false;
@override
String toString() =>
'Task:: id = $id , Title = $taskTitle , is Done = $isDone';
@override
int compareTo(covariant Task other) => other.id.compareTo(id);
}
Database functions class
class TaskDatabase {
final String dbName;
Database? _db;
List<Task> _tasksList = [];
final _streamController = StreamController<List<Task>>.broadcast();
TaskDatabase({required this.dbName});
Stream<List<Task>> all() =>
_streamController.stream.map((tasks) => tasks..sort());
// opening database function
// closing database function
// fetching database data function
// deleting database data and updating etc...
}
I am using a List<Task>
to cache
the fetched data from the database and used a streamBuilder
to render the UI from that list data.
Sample database function that existing inside the database functions class (Open DB Func)
Future<bool> open() async {
if (_db != null) {
return true;
}
final directory = await getApplicationDocumentsDirectory();
final path = '${directory.path}/$dbName';
try {
final db = await openDatabase(path);
_db = db;
//creating the database table using sqflite
const createTable = '''CREATE TABLE IF NOT EXISTS "TABLEOFTASKS" (
"id" INTEGER NOT NULL,
"taskTitle" TEXT,
"isDone" INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY("id" AUTOINCREMENT));''';
await db.execute(createTable);
// read all existing task objects from the db
_tasksList = await _fetchTasks();
_streamController.add(_tasksList);
return true;
} catch (e) {
print('error = $e');
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用它来获取所有任务的计数
,并使用它来完成所有完成任务,
不确定它们是否在这种方式上工作,但是我在mySQL命令行中使用此查询,并且正常工作正常
try to use this to get count of all tasks
and use this to get all done tasks
am not sure if they work on flutter that way , but i use this query in MySql Command Line and it work fine