Flutter - function outputs zero even though value from dbhelper is present

74 Views Asked by At

I have an where i cant pass on the value obtained from my sqflight db to my method in a stateless function. My db works and i obtain the right value which i checked through the print function no issue here.

for context in dbhelper:

  static Future <int>  getWaterDrank() async {
    final db = await WaterDBHelper.db();
     List result = await db.rawQuery(
        "SELECT * FROM $tableName WHERE ${columnDate} = ?", [dateCurrent]
    );
     print('this is the result $result');
    String waterDrank = result[0]
        .entries
        .firstWhere((element) => element.key == 'waterDrank')
        .value.toString();

    print('this is what we are after ${int.parse(waterDrank)}');

    return int.parse(waterDrank);
  }

this is my stateless widget, where there is this function in charge of deriving value from db. for some reason i get it as zero even though the print function in dbhelper displays a value.

  getWaterDrankStored()  async {
   int waterStoredValueInteger =  await WaterDBHelper.getWaterDrank();

    return waterStoredValueInteger;
  }

i am not sure what i am doing wrong. enter image description here

2

There are 2 best solutions below

2
On

I guess, your GetWaterDrankStored() method is returning something else, but not int. And you are getting an error when you are trying to call this method. Make sure your method returns Future<int>.

2
On

It seems like you did not provide a return value to your async method. This return value should be a future. Also the variable totalWaterDrank has an internal Scope inside the method. Please try this:

  Future<int> totalWaterDrank2;

  
    
  Future<int> addedWater()  async{
    int totalWaterDrank = await WaterButtonsGrouped().getWaterDrankStored();
    
    return  totalWaterDrank;
  }
    
    totalWaterDrank2 = addedWater();