使用颤振中的 Streambuilder 从 SQFLITE 获取列总和

时间:2021-05-19 11:25:01

标签: flutter sqflite

我开始学习 flutter,目前正在使用 SQFLITE 进行 Simple CRUD 操作项目。我有交易表,其中有 6 列(id、transactiontype、date、Category、Amount、notes)。所以,我想在 StreamBuilder 的帮助下获得总金额列的总和。我试过了,但它对我不起作用,请在这里帮助我,我很高兴.....

这是我的模型---

class TransactionModel {
  int id;
  String type;
  String date;
  String categoryid;
  String amount;
  String notes;

  TransactionModel(
      {this.id,
      this.type,
      this.date,
      this.categoryid,
      this.amount,
      this.notes});

  factory TransactionModel.fromDatabaseJson(Map<String, dynamic> data) =>
      TransactionModel(
          id: data['id'],
          type: data['type'],
          date: data['date'],
          categoryid: data['categoryid'],
          amount: data['amount'],
          notes: data['notes']);

  Map<String, dynamic> toDatabaseJson() => {
        'id': this.id,
        'type': this.type,
        'date': this.date,
        'categoryid': this.categoryid,
        'amount': this.amount,
        'notes': this.notes
      };
}

这是我未来计算总金额列总和的功能----

Future getsumofIncome(String type) async {
    final db = await dbProvider.database;
    var incomeresult = await db.rawQuery(
        "SELECT SUM(amount) FROM  $transactionTABLE WHERE type = ?", ["$type"]);
    int incomevalue = incomeresult[0]['SUM(amount)'];
    print('Result of Income Sum Amount  $incomevalue');
    // print(incomeresult.toList());
    return incomevalue;
  }

这是我的 UI 部分,用于显示从数据库文件中获取的数据。我正在为它使用 StreamBuilder,但它在我看来不起作用..... 让我在这里提到我也在我的项目中使用 bloc 模式我只是把我的逻辑函数放在上面,你检查我如何计算值...

                stream: transactionBloc.getsumofIncome('Income'),
                builder: (BuildContext context,
                    AsyncSnapshot<TransactionModel> incomesnapshot) {
                  if (incomesnapshot.hasData) {
                    return Container(
                      padding: EdgeInsets.all(10.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            'Income',
                            style: TextStyle(fontSize: 20.0),
                          ),
                          Text(
                            incomesnapshot != null
                                ? '₹ ${incomesnapshot.toString()}'
                                : '0',
                            style:
                                TextStyle(fontSize: 20.0, color: Colors.green),
                          ),
                        ],
                      ),
                    );
                  } else {
                    CircularProgressIndicator();
                  }
                }),

0 个答案:

没有答案