Search code examples
flutterdartsqflite

Flutter: sqflite sum amount is null in text widget


Disclaimer, I am quite new to coding in general, but I have tried researching for several days and also using the answers to several similar questions on here but none of them seem to be working. I appreciate your patience with a beginner.

I am using raw query in my database file as follows

 Future getTotal() async {
    var db = await this.database;
    var result =
        await db.rawQuery("SELECT SUM(amount) as sum FROM $expensesTable");
    print(result.toString());
    return result;
  }

I am trying to display the sum of the "amount" column in my database in a text widget. Here is the code for that UI and text widget:

class BudgetAppState extends State<BudgetApp> {
  ExpensesDatabase databaseHelper;
  ExpensesDatabase expensesDatabase = ExpensesDatabase();
  List<Expenses> expensesList;
  double getTotal;

  @override
  Widget build(BuildContext context) {
    return new WillPopScope(
      onWillPop: () async => false,
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text("Budget App"),
          centerTitle: true,
          backgroundColor: Colors.blueAccent,
        ),
        drawer: NavigationDrawer(),
        body: Text(getTotal.toString()),
      ),
    );
  }

  void displayTotal() async {
    var total = await databaseHelper.getTotal();
    setState(() {
      print('$total');
      getTotal = total;
    });
  }
}

The individual row data from the "amount" column in my database is showing up in a different List view widget I have on a different page in my app, but when I try to use this to SUM all the "amounts", the text for this is showing up as null. Any help would be greatly appreciated!


Solution

  • You're never actually initializing your getTotal property by calling your displayTotal method. When you're initializing the state of your widget make sure to call displayTotal(). Once it's done it will rebuild your widget and show you the value of getTotal.

    
    class BudgetAppState extends State<BudgetApp> {
      ExpensesDatabase databaseHelper;
      ExpensesDatabase expensesDatabase = ExpensesDatabase();
      List<Expenses> expensesList;
      double getTotal;
    
      @override
      void initState() {
        super.initState();
        displayTotal();
      }
    
      @override
      Widget build(BuildContext context) {
        return new WillPopScope(
          onWillPop: () async => false,
          child: new Scaffold(
            appBar: new AppBar(
              title: new Text("Budget App"),
              centerTitle: true,
              backgroundColor: Colors.blueAccent,
            ),
            drawer: NavigationDrawer(),
            body: Text(getTotal?.toString() ?? 'Loading...'),
          ),
        );
      }
    
      void displayTotal() async {
        var total = await databaseHelper.getTotal();
        setState(() {
          print('$total');
          getTotal = total;
        });
      }
    }