1

I am trying get sum of a column in sqlite table using Bloc pattern.

debt_bloc.dart

getTotalAmount() async {
    return await _debtRepository.getTotalAmt();
  }

debt_dao.dart

 Future<int> getTotalAmount() async {
    final db = await dbProvider.database;

    var result = await db.rawQuery("SELECT SUM(amount) FROM $debtDetailsTable");
    int value = result[0]["SUM(amount)"];
    return value;
  }

debt_repositary.dart

Future getTotalAmount() => debtDao.getTotalAmount();

When i try to print like below

var total;
   @override
    void initState () {
      super.initState();
     _asyncMethod();
    }

    _asyncMethod() async {
      var t = await debtBloc.getTotalAmount();
      setState(() {
        total = t;
      });
    }

    print(total);

Output not updating when add new data. But if go back to home screen and come to respective screen value is updating.

Please guide me in right way. Thanks in advance

1 Answer 1

1

Await on your method first before printing it.

var total = await debtBloc.getTotalAmount(); // await here
print(total); // should now print some int value
Sign up to request clarification or add additional context in comments.

8 Comments

Now that is solved but output value is "null". I am new to flutter dn't know how null is coming. Trying to fix it, If you have any idea about it, help me out.
While tried to print in debt_dao.dart. var result = await db.rawQuery("SELECT SUM(amount) FROM $debtDetailsTable"); print(result); output comes correct.
What int value = result[0]["SUM(amount)"]; prints?
It prints sum of amount column value. Ex. 30000
@Nagaraj I understand what it should print but can you tell me what exactly the current value prints.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.