title: How do I get data from an sqflite table and display it as a % inside text widget
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import 'dart:io';
  
  import 'package:path/path.dart';
  import 'package:sqflite/sqflite.dart';
  import 'package:path_provider/path_provider.dart';
  
  class DatabaseHelper {
  
    static final _databaseName = "MyDatabase.db";
    static final _databaseVersion = 1;
  
    static final table = 'my_table';
  
    static final columnId = '_id';
    static final columnName = 'name';
    static final columnAge = 'age';
  
    // make this a singleton class
    DatabaseHelper._privateConstructor();
    static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
  
    // only have a single app-wide reference to the database
    static Database _database;
    Future&lt;Database&gt; get database async {
      if (_database != null) return _database;
      // lazily instantiate the db the first time it is accessed
      _database = await _initDatabase();
      return _database;
    }
  
    // this opens the database (and creates it if it doesn't exist)
    _initDatabase() async {
      Directory documentsDirectory = await getApplicationDocumentsDirectory();
      String path = join(documentsDirectory.path, _databaseName);
      return await openDatabase(path,
          version: _databaseVersion,
          onCreate: _onCreate);
    }
  
    // SQL code to create the database table
    Future _onCreate(Database db, int version) async {
      await db.execute('''
            CREATE TABLE $table (
              $columnId INTEGER PRIMARY KEY,
              $columnName TEXT NOT NULL,
              $columnAge INTEGER NOT NULL
            )
            ''');
    }
  
    // Helper methods
  
    // Inserts a row in the database where each key in the Map is a column name
    // and the value is the column value. The return value is the id of the
    // inserted row.
    Future&lt;int&gt; insert(Map&lt;String, dynamic&gt; row) async {
      Database db = await instance.database;
      return await db.insert(table, row);
    }
  
    // All of the rows are returned as a list of maps, where each map is
    // a key-value list of columns.
    Future&lt;List&lt;Map&lt;String, dynamic&gt;&gt;&gt; queryAllRows() async {
      Database db = await instance.database;
      return await db.query(table);
    }
  
    // All of the methods (insert, query, update, delete) can also be done using
    // raw SQL commands. This method uses a raw query to give the row count.
    Future&lt;double&gt; queryRowCount() async {
      Database db = await instance.database;
      List&lt;Map&lt;String, dynamic&gt;&gt; x = await db.rawQuery('SELECT COUNT(*) FROM $table');
      int rowCount = Sqflite.firstIntValue(x);
      return rowCount.toDouble();
    }
  
    // We are assuming here that the id column in the map is set. The other
    // column values will be used to update the row.
    Future&lt;int&gt; update(Map&lt;String, dynamic&gt; row) async {
      Database db = await instance.database;
      int id = row[columnId];
      return await db.update(table, row, where: '$columnId = ?', whereArgs: [id]);
    }
  
    // Deletes the row specified by the id. The number of affected rows is
    // returned. This should be 1 as long as the row exists.
    Future&lt;int&gt; delete(int id) async {
      Database db = await instance.database;
      return await db.delete(table, where: '$columnId = ?', whereArgs: [id]);
    }
  
    Future&lt;List&lt;Map&lt;String, dynamic&gt;&gt;&gt; queryOmnivore() async {
      Database db = await instance.database;
      return await db.query(table, where: '$columnName = ?', whereArgs: ['omnivore']);
    }
  
    Future&lt;List&lt;Map&lt;String, dynamic&gt;&gt;&gt; queryPescatarian() async {
      Database db = await instance.database;
      return await db.query(table, where: '$columnName = ?', whereArgs: ['pescatarian']);
    }
  
    Future&lt;List&lt;Map&lt;String, dynamic&gt;&gt;&gt; queryVegetarian() async {
      Database db = await instance.database;
      return await db.query(table, where: '$columnName = ?', whereArgs: ['vegetarian']);
    }
  
    Future&lt;int&gt; queryVegetarianCount() async {
      var vegList = await queryVegetarian();
      int count = vegList.length;
      return count;
    }
  
    Future&lt;double&gt; queryOmnivoreCount() async {
      var omniList = await queryOmnivore();
      int omniCount = omniList.length;
      return omniCount.toDouble();
    }
  
    Future&lt;double&gt; calcOmnivorePercentage() async {
      var x = await queryOmnivoreCount();
      var y = await queryRowCount();
      double omniPercentage = (x / y) * 100;
      return omniPercentage;
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Hey Folks!</p>
  
  <p>I was hoping someone may be able to help me please?!</p>
  
  <p>I'm trying to figure out how to take data out of a a sqflite table I've created, perform a calculation that expresses it as a percentage of the other values, and display it inside a text widget in the app.</p>
  
  <p>I've actually managed to get the result to print in the console using this code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>void omnivorePercentageQ() async {
      final omni = await dbHelper.calcOmnivorePercentage();
      print('omnivore percentage: ${omni.toStringAsFixed(1)}');
    } </code></pre>
  </div>
  </div>
  </p>
  
  <p>But I have no idea how to get it to show up in a text widget in the app itself. </p>
  
  <p>Any ideas would be greatly appreciated!</p>
  
  <p>Thank you,</p>
  
  <p>Jason</p>