如何在 flutter 中从 sqlite 数据库中获取两个日期之间的所有记录?

How to get all the records between two dates from sqlite database in flutter?

我正在尝试开发一个应用程序,用户可以在其中保存他们的交易和交易日期、金额、类别等相关信息。所有这些字段都使用 sqflite 插件[存储在 SQLite 数据库中] =26=]。在那方面,我想实现一个查看选项,用户可以在其中查看他们在两个日期之间的交易。 我做了以下从数据库中获取数据的函数:

Future<List<expense>> getExpenseDateWise() async {
final db = await database;
var expenses = await db
    .rawQuery('SELECT * FROM EXPENSES WHERE DATE(DATETIME) >= ? AND DATE(DATETIME) <= ?',
['$FromDate','$ToDate']); // <----- FromDate and ToDate are two DateTime variables which I am saving from a form

List<expense> expenseList = List<expense>();
expenses.forEach((currentExpense) {
  expense expenses = expense.fromMap(currentExpense);

  expenseList.add(expenses);
});
return expenseList;
}

我已经在 android studio 中使用 darabase 检查器检查了查询,它正在运行。 然后显示我已经实现了以下代码:

class _dateWiseViewState extends State<dateWiseView> {
 @override
 void initState() {
 super.initState();
 DatabaseProvider.db.getExpenseDateWise().then(
      (expenseList) {
    BlocProvider.of<ExpenseBloc>(context).add(SetDates(expenseList));
  },);}
Widget build(BuildContext context) {
String finalFrom = DateFormat('yyyy-MM-dd').format(widget.fromDate);
String finalTo = DateFormat('yyyy-MM-dd').format(widget.toDate);
return Scaffold(
  appBar: AppBar(
    title: Text("$finalFrom - $finalTo"),
  ),
  body: Container(
      child: BlocConsumer<ExpenseBloc, List<expense>>(
          builder: (context, expenseList) {
            return ListView.separated(
                itemBuilder: (BuildContext context, int index) {
                  expense expensess = expenseList[index];
                  return Container(
                      margin: const EdgeInsets.all(5.0),
                      padding: const EdgeInsets.all(10),
                      decoration: BoxDecoration(
                          color: index % 2 == 0 ? Colors.white : Colors.lightBlueAccent,
                          border: Border.all(width: 2,color: Colors.white),
                          borderRadius: BorderRadius.circular(15)
                      ),
                      child: ListTile(
                          leading: CircleAvatar(
                            backgroundColor: Colors.indigo,
                            foregroundColor: Colors.white,
                            child: Text(
                              expensess.name
                                  .substring(0, 1)
                                  .toUpperCase(),
                              style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
                            ),
                          ),
                          title: Text(
                            expensess.name ?? "Title",
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 30,
                              color: Colors.black,
                            ),
                          ),
                          subtitle: Text(
                            "Amount: ${expensess.amount.toString()}"
                                "\nDate: ${expensess.pickedDate}\n"
                            ,style: TextStyle(
                            fontStyle: FontStyle.italic,
                            fontSize: 20,
                            color: Colors.black,
                          ),
                          ),
                          onTap: () {
                          }
                      )
                  );
                },
              itemCount: expenseList.length,
              separatorBuilder: (BuildContext context, int index) => Divider(color: Colors.black),
                );
          },
        listener: (BuildContext context, expenseList) {},
       ),),);}
    }

我希望这段代码能正常工作,但它 return 列出了列表,但它还包括其他月份的交易,请仔细查看下图 (注意日期) :

我还附上了下图,说明查询正在使用数据库检查器:

查看数据库检查器,具有相同查询的页面数据库检查器显示 2 个条目,而页面显示 4 个条目。 我不知道为什么会这样,请帮助我。 感谢您的回复。

兄弟,你检查过你的查询是否正确地完成了它的工作吗?

  Future<List> getRangeData(String fromDate, String toDate) async{
   var dbClient = await db;
   var result = await dbClient
        .rawQuery("SELECT * FROM $tableName where $columnDate >= '2021-01-01' and 
    $columnDate <= '2022-10-10' ");
   return result.toList();
  }