title: Flutter: passing future<int> as int to Charts
body: <p>I am making a mood checker application using flutter where user chose one of 5 emojis to tell their mood. 
  I want to display a PieChart with data emoji vs num of Days it has been chosen.
  Problem is that I have to fetch data from sqflite Database to get numOfDays a particular emoji is chosen which will be of type Future but charts can't take Futures, I tried to use async-await but it didn't seem to work.</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>E/flutter (27721): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'Future&lt;dynamic&gt;' is not a subtype of type 'int'
  E/flutter (27721): #0      _MoodChartState.initData (package:pro_app/journal/view/mood_chart.dart:36:24)
  E/flutter (27721): #1      _AsyncAwaitCompleter.start (dart:async/runtime/libasync_patch.dart:49:6)
  E/flutter (27721): #2      _MoodChartState.initData (package:pro_app/journal/view/mood_chart.dart:31:16)
  E/flutter (27721): #3      _MoodChartState.initState (package:pro_app/journal/view/mood_chart.dart:58:5)
  E/flutter (27721): #4      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3846:58)
  E/flutter (27721): #5      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
  E/flutter (27721): #6      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
  E/flutter (27721): #7      Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
  E/flutter (27721): #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
  E/flutter (27721): #9      Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
  E/flutter (27721): #10     StatelessElement.update (package:flutter/src/widgets/framework.dart:3796:5)
  E/flutter (27721): #11     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #12     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
  E/flutter (27721): #13     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
  E/flutter (27721): #14     ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
  E/flutter (27721): #15     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #16     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
  E/flutter (27721): #17     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
  E/flutter (27721): #18     ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
  E/flutter (27721): #19     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #20     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4601:32)
  E/flutter (27721): #21     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4992:17)
  E/flutter (27721): #22     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #23     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
  E/flutter (27721): #24     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
  E/flutter (27721): #25     StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
  E/flutter (27721): #26     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #27     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
  E/flutter (27721): #28     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
  E/flutter (27721): #29     ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
  E/flutter (27721): #30     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #31     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
  E/flutter (27721): #32     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
  E/flutter (27721): #33     StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
  E/flutter (27721): #34     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #35     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4883:14)
  E/flutter (27721): #36     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #37     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
  E/flutter (27721): #38     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
  E/flutter (27721): #39     StatelessElement.update (package:flutter/src/widgets/framework.dart:3796:5)
  E/flutter (27721): #40     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #41     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4883:14)
  E/flutter (27721): #42     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
  E/flutter (27721): #43     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
  E/flutter (27721): #44     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
  E/flutter (27721): #45     StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
  E/flutter (27721): #46     Element.updateChild (package:flutter/src/wid
  </code></pre>
  
  <p><strong>Mood Model file</strong></p>
  
  <p>import 'package:flutter/material.dart';</p>
  
  <pre><code>class Mood {
    int _moodID;
    int _emojiID;
    int _actID;
    int _moodDay;
    int _moodMonth;
    int _moodYear;
  
    Mood(this._emojiID, this._moodDay, this._moodMonth, this._moodYear,
        this._actID);
  
    Mood.withId(this._moodID, this._emojiID, this._moodDay, this._moodMonth,
        this._moodYear, this._actID);
  
    int get moodID =&gt; _moodID;
    int get emojiID =&gt; _emojiID;
    int get actID =&gt; _actID;
    int get moodDay =&gt; _moodDay;
    int get moodMonth =&gt; _moodMonth;
    int get moodYear =&gt; _moodYear;
  
  // can define setters as well
  
    Map&lt;String, int&gt; toMap() {
      var map = Map&lt;String, int&gt;();
      if (_moodID != null) {
        map['moodId'] = _moodID;
      }
      map['emojiId'] = _emojiID;
      map['actId'] = _actID;
      map['moodDay'] = _moodDay;
      map['moodMonth'] = _moodMonth;
      map['moodYear'] = _moodYear;
      return map;
    }
  
    Mood.fromMap(Map&lt;String, int&gt; map) {
      this._moodID = map['moodId'];
      this._emojiID = map['emojiId'];
      this._actID = map['actId'];
      this._moodDay = map['moodMap'];
      this._moodMonth = map['moodMonth'];
      this._moodYear = map['moodYear'];
    }
  }
  </code></pre>
  
  <p><strong>*Database file</strong></p>
  
  <pre><code>import 'package:path/path.dart';
  import 'dart:async';
  import 'package:path_provider/path_provider.dart';
  import 'package:sqflite/sqflite.dart';
  import 'dart:io';
  
  import '../models/mood_model.dart';
  
  class DatabaseHelper {
    static final DatabaseHelper _instance = new DatabaseHelper.internal();
    factory DatabaseHelper() =&gt; _instance;
  
    final String tableName = "moodTable";
    final String colMoodId = "moodId";
    final String colEmojiId = "emojiId";
    final String colActId = "actId";
    final String colMoodDay = "moodDay";
    final String colMoodMonth = "moodMonth";
    final String colMoodYear = "moodYear";
  
    static Database _db;
  
    Future&lt;Database&gt; get db async {
      if (_db == null) {
        return initDb();
      }
      return _db;
    }
  
    DatabaseHelper.internal();
  
    Future&lt;Database&gt; initDb() async {
      Directory documentDirectory = await getApplicationDocumentsDirectory();
      String path = join(documentDirectory.path, "mood.db");
      var ourDb = await openDatabase(path, version: 1, onCreate: _onCreate);
      return ourDb;
    }
  
    void _onCreate(Database db, int version) async {
      await db.execute(
          "CREATE TABLE moodTable (moodId INTEGER PRIMARY KEY, emojiId INTEGER, actId INTEGER, moodDay INTEGER, moodMonth INTEGER, moodYear INTEGER)");
      print("Table Created");
    }
  
    // insert
    Future&lt;int&gt; saveMood(Mood mood) async {  
      var dbClient = await this.db;
      int result = await dbClient.insert("$tableName", mood.toMap());
      print('Saved');
      return result;
  
    }
  
  
  
    // to get number of mood days
    Future&lt;List&lt;Map&lt;String, dynamic&gt;&gt;&gt; listOfMoods() async {
      Database db = await this.db;
      var result = await db.query(tableName, orderBy: '$colMoodId ASC');
      return result;
    }
  
    Future&lt;int&gt; numOfMoodDays(int emojiID) async {
      Database db = await this.db;
      var result = await db.query(tableName,
          orderBy: '$colMoodId ASC',
          where: '$colEmojiId = ?',
          whereArgs: [emojiID]);
      return result.length;
    }
  
    // close db
    Future close() async {
      var dbClient = await db;
      return dbClient.close();
    }
  }
  </code></pre>
  
  <p><strong>Mood_Chart file snippet</strong></p>
  
  <pre><code>    import 'package:flutter/material.dart';
      import 'dart:math';
      import 'package:charts_flutter/flutter.dart' as charts;
      import '../view/mood.dart';
      import '../models/mood_db.dart';
      import 'dart:async';
  
      class Mood {
        String _emoji;
        int _numOfDays; // this has to int bcz charts can't take future&lt;int&gt;
        charts.Color _color;
  
        Mood(this._emoji, this._numOfDays, this._color);
      }
  
      class MoodChart extends StatefulWidget {
        @override
        _MoodChartState createState() =&gt; _MoodChartState();
      }
  
      class _MoodChartState extends State&lt;MoodChart&gt; {
        DatabaseHelper _databaseHelper = DatabaseHelper();
        List&lt;Mood&gt; _data;
        List&lt;charts.Series&lt;Mood, String&gt;&gt; _chartData;
  
        // Here I used async-await but this function still return Future&lt;int&gt;    
        numOfDaysFn(int id) async {
          int numOfDays =await _databaseHelper.numOfMoodDays(id);
          return numOfDays;
        }
  
        void initData() async {
          _chartData = List&lt;charts.Series&lt;Mood, String&gt;&gt;();
  
          _data = &lt;Mood&gt;[
            Mood(
                allMoods[0], numOfDaysFn(1), charts.MaterialPalette.red.shadeDefault),
            Mood(allMoods[1], numOfDaysFn(2),
                charts.MaterialPalette.blue.shadeDefault),
            Mood(allMoods[2], numOfDaysFn(3),
                charts.MaterialPalette.gray.shadeDefault),
            Mood(allMoods[3], numOfDaysFn(4),
                charts.MaterialPalette.indigo.shadeDefault),
            Mood(allMoods[4], numOfDaysFn(5),
                charts.MaterialPalette.green.shadeDefault),
          ];
          _chartData.add(charts.Series(
            id: 'Mood',
            data: _data,
            colorFn: (Mood mood, _) =&gt; mood._color,
            domainFn: (Mood mood, _) =&gt; mood._emoji,
            measureFn: (Mood mood, _) =&gt; **mood._numOfDays**, //this can't take futures
          ));
        }
  
        @override
        void initState() {
          super.initState();
          initData();
        }
  
        @override
        Widget build(BuildContext context) {
          return Container(
            child: charts.PieChart(
              _chartData,
              animate: true,
            ),
          );
        }
      }
  List allMoods = ['Disgusting', 'Bad', 'Ok', 'Good', 'Amazing'];
  </code></pre>