我有一个简单的小部件,它可以监听记录的当月费用.这是建立与河舱和漂流包在Flutter .我使用的数据模式是提供者&>存储库&>DAO.

然而,由于某些原因,当我在本地数据库中添加或删除与当前月份匹配的交易时(通过查看SQLite表进行验证),小部件不会使用最新的ExpendsTo Date值进行更新.看起来,这条溪流似乎没有发出新的价值.我已经试着调试这个程序很久了,但不知道出了什么问题.

预期行为:

  1. 交易记录表中的记录被修改(增加或删除)
  2. ExpensesToDateProvider发出新值
  3. 重新构建小部件以反映新价值
// Widget to display latest expenseToDate
class DebugExpensesWidget extends ConsumerWidget {
  const DebugExpensesWidget({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final expensesAsyncValue =
        ref.watch(expensesToDateProvider(budgetDate: DateTime(DateTime.now().year, DateTime.now().month)));

    return expensesAsyncValue.when(
      data: (data) {
        return Text('Expenses to date: $data');
      },
      loading: () => CircularProgressIndicator(),
      error: (error, _) {
        return Text('Error: $error');
      },
    );
  }
}

// Provider that watches spentToDate for the month
@riverpod
Stream<double> expensesToDate(ExpensesToDateRef ref, {required DateTime budgetDate}) {
  return ref.watch(budgetsRepositoryProvider).getExpensesToDate(budgetDate);
}
// Repository to call the DAO to return total expenses to date based on a given budgetDate
Stream<double> getExpensesToDate(DateTime budgetDate) {
  return database.appDatabaseDao.calculateExpensesToDateDao(budgetDate.year, budgetDate.month);
  }
  // DAO to calculate Total Expenses to date based on a given month and year
  Stream<double> calculateExpensesToDateDao(int year, int month) {
    final yearStr = year.toString();
    final monthStr = month.toString().padLeft(2, '0');

    const sql = '''
      SELECT SUM(amount) as total
      FROM transactions
      WHERE is_income = 0
      AND strftime('%m', datetime(transaction_date, 'unixepoch')) = ?
      AND strftime('%Y', datetime(transaction_date, 'unixepoch')) = ?
    ''';

    return customSelect(sql, variables: [
      Variable.withString(monthStr),
      Variable.withString(yearStr),
    ]).watch().map((rows) {
      final row = rows.first;
      final total = row.read<double?>('total') ?? 0.0;
      return total;
    });
  }

推荐答案

在漂移中使用customSelectwatch时,需要指定readsFrom.从docs人开始:

对于自动更新的查询流,需要将readsFrom参数设置为SQL语句从中读取的表-漂移不能像使用其Dart API构造的其他查询那样自动推断它.

假设您的DAO上有transactions个表可用,下面的内容应该会使流正确更新:

return customSelect(
  sql,
  variables: [
    Variable.withString(monthStr),
    Variable.withString(yearStr),
  ],
  readsFrom: {transactions},
).watch().map((rows) {
  // ...

Flutter相关问答推荐

为什么我不能看到类音频源在flutter?'

底部导航栏项目在抖动中不更改 colored颜色

GitHub Actions工作流secret中的特殊字符未被保留

FittedBox叠加技术在Flutter 中的应用

如何在Flutter Webview包中触发基于URL导航的事件

如何更改Flutter 中文本的混合模式?

网页浏览器不支持鼠标滚动的行项目

如何在 Flutter 中测试动画图标

当项目较少时收缩列表视图

在 Flutter 中读取 Firebase 数据库数据时遇到问题

访问 AsyncSnapshot 中的嵌套属性

flutter - 无法解析 JSON 消息:文档为空

Flutter snapShot.hasData 为假但响应有数据

如何限制用户每 24 小时执行的操作数?

如何在 flutter 中创建渐变按钮

我正在try 使用 Firebase 在 Flutter 中使用 Google 注销,但它不起作用

如何在flutter中共享容器和gridview之间的滚动条?

根据索引/变量更改小部件的 colored颜色

这在Flutter 的 pubspec 文件中是什么意思?

RangeError(索引):无效值:有效值范围为空:点击文章时为0