title: Count records and return total records number in real time then display it as text in a widget
body: <p>I'm trying to get total number of records sqlite database table called 'persons'. After getting the total number of records I want to show this number in a <code>Container</code> widget inside a <code>Text()</code> widget. I have tried different ways to make this work but not any method I've used is working.</p>
  
  <p>Below is <strong>db_helper.dart</strong> file. The funtion that needs to return total number of records is being defined as <code>countPeople</code>.</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:async';
  import 'dart:io' as io;
  import 'package:path_provider/path_provider.dart';
  import 'package:sqflite/sqflite.dart';
  
  /// models
  
  import 'package:path/path.dart';
  
  class DBHelper {
    static Database db_instance;
  
    Future&lt;Database&gt; get db async {
      if (db_instance == null) {
        db_instance = await initDB();
      }
      return db_instance;
    }
  
    initDB() async {
      var databasesPath = await getDatabasesPath();
      String path = join(databasesPath, 'development.db');
      var db = await openDatabase(path, version: 1, onCreate: onCreateFunc);
      return db;
    }
  
    void onCreateFunc(Database db, int version) async {
      // create table
      await db.execute('''CREATE TABLE persons(
        id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, nickname TEXT, country_id INTEGER, role TEXT, created DATETIME, updated DATETIME
      );''');
    }
  
    /**
     * crud functions
     */
  
    void countPeople() async {
      var db_connection = await db;
      final count = Sqflite.firstIntValue(
          await db_connection.rawQuery('SELECT COUNT(*) FROM persons'));
      // assert(count == 2);
      print('people count: $count');
    }
    Future&lt;List&lt;Person&gt;&gt; getAllPersons() async {
      final db_connection = await db;
      var response = await db_connection
          .rawQuery("SELECT * FROM persons ORDER BY created DESC");
      // print(response);
      List&lt;Person&gt; list = response.map((c) =&gt; Person.fromJson(c)).toList();
      return list;
    }
  
    // add new person
    void addNewPerson(Person person) async {
      var db_connection = await db;
      String query = """
          INSERT INTO persons(name,nickname,role,created,updated) VALUES('${person.name}','${person.nickname}', '${person.role}','${person.created}', '${person.updated}')
          """;
      await db_connection.transaction((transaction) async {
        // print(query);
        return await transaction.rawInsert(query);
      });
    }
  
  </code></pre>
  
  <p>Below is <strong>people_count.dart</strong> file. This is where I want the total record count number to be shown inside the <code>Text()</code> widget.</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:flutter/material.dart';
  import 'package:com.example.simple_app/utils/db_helper.dart';
  import 'package:com.example.simple_app/models/person.dart';
  import 'package:com.example.simple_app/pages/main_user_registration/create_account.dart';
  
  class MainUserRegistration extends StatefulWidget {
    MainUserRegistration({Key key}) : super(key: key);
  
    @override
    _MainUserRegistrationState createState() =&gt; _MainUserRegistrationState();
  }
  
  var dbHelper = DBHelper();
  
  class _MainUserRegistrationState extends State&lt;MainUserRegistration&gt; {
    var result = dbHelper.countPeople();
  
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: Text('Loans'),
        ),
        body: new Container(
          child: Row(
            children: &lt;Widget&gt;[
              Text('all persons: ${dbHelper.countPeople()}'), // the total number of records should appear here
            ],
          ),
        ),
      );
    }
  }
  
  </code></pre>