1

我正在构建一个金融应用程序,创建了一个名为 wallets 的表,该 wallets 表有一个名为startDate. 为 SQLite 使用 (flutter_moor) 包

现在我只需要检索最新创建的钱包,并且只有在使用上述字段的值创建它以来的 30 天范围内startDate,这意味着在新月份我无法获取它

我的代码:

Stream<Wallet> watchLatestCreatedWallet() {
  final Stream<Wallet> wallet = (
    select(wallets)
    ..orderBy([
      (wallet) => OrderingTerm(
        expression: wallet.startDate,
        mode: OrderingMode.desc
      ) 
    ])
    // somthing wrong I did, obviously always will return true
    // ..where((tbl) => tbl.startDate.isBetween(tbl.startDate, tbl.startDate + Duration(days: 30) ) )
    ..limit(1)
  )
  .watchSingle();

  return wallet;
}
4

1 回答 1

0

修复了这个

Stream<Wallet> watchLatestCreatedWallet() {
  final endRange = DateTime.now();
  final startRange = endRange.subtract(Duration(days: 30));
  final Stream<Wallet> wallet = (
    select(wallets)
    ..orderBy([
      (wallet) => OrderingTerm(
        expression: wallet.startDate,
        mode: OrderingMode.desc
      ) 
    ])
    ..where((tbl) => tbl.startDate.isBetween(startRange, endRange ) )
    ..limit(1)
  )
  .watchSingle();

  return wallet;
}

于 2021-06-20T20:13:00.900 回答