Nagaraj
Nagaraj

Reputation: 137

Getting output as "Instance of 'Future<dynamic>'" in flutter

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

Upvotes: 1

Views: 2934

Answers (1)

CopsOnRoad
CopsOnRoad

Reputation: 268244

Await on your method first before printing it.

var total = await debtBloc.getTotalAmount(); // await here
print(total); // should now print some int value

Upvotes: 1

Related Questions