How to Populate ListView with Database in Flutter

In this Google flutter code example we are going to learn how to populate ListView with Database in Flutter.

The code is based on flutter version 1.0.0

Main.dart (App entry point)

import 'package:flutter/material.dart';
import 'listview_sqlite_db_task-6.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        // This is the theme of your application.
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
      //home: MyHomePage(title: 'Flutter Demo Home Page'),
      home: ListViewSqliteDB(),


class UserModel {
  String username;
  String name;
  String email;
  String password;


  UserModel.fromMap(dynamic obj) { = obj["name"];
    this.username = obj["username"]; = obj["email"];
    this.password = obj["password"];

  Map<String, dynamic> toMap() {
    var map = new Map<String, dynamic>();

    map["username"] = username;
    map["name"] = name;
    map["email"] = email;
    map["password"] = password;

    return map;

  String get getName => name;
  String get getUsername => username;
  String get getPassword => password;
  String get getEmail => email;


import 'dart:async';
import 'dart:io' as io;

import './user_model.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';

class DatabaseHelper {
  static final DatabaseHelper _instance = new DatabaseHelper.internal();
  factory DatabaseHelper() => _instance;

  static Database _db;

  Future<Database> get db async {
    if (_db != null) return _db;
    _db = await initDb();
    return _db;


  initDb() async {
    io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, "main.db");
    var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
    return theDb;

  void _onCreate(Database db, int version) async {
    await db.execute("""CREATE TABLE user(
        username TEXT,
        email TEXT,
        name TEXT,
        password TEXT

  Future<int> saveData(UserModel user) async {
    var dbClient = await db;
    int res = await dbClient.insert("user", user.toMap());
    return res;

  Future<List<UserModel>> getUserModelData() async {
    var dbClient = await db;
    String sql;
    sql = "SELECT * FROM user";

    var result = await dbClient.rawQuery(sql);
    if (result.length == 0) return null;

    List<UserModel> list = {
      return UserModel.fromMap(item);

    return list;


import 'package:flutter/material.dart';
import 'package:db_helper.dart';
import 'package:user_model.dart';

class ListViewSqliteDB extends StatefulWidget {
  _ListViewSqliteDBState createState() => _ListViewSqliteDBState();

class _ListViewSqliteDBState extends State<ListViewSqliteDB> {
  DatabaseHelper db = DatabaseHelper();

  //Add data to db
  _saveData() async {
    UserModel user1 = UserModel(
      "Jude Jonathan",

    UserModel user2 = UserModel(
    await db.saveData(user1);
    await db.saveData(user2);

  void initState() {

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Fetching data from DB - ListView'),
      body: FutureBuilder<List<UserModel>>(
        future: db.getUserModelData(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return Center(child: CircularProgressIndicator());

          return ListView(
                .map((user) => ListTile(
                      title: Text(,
                      subtitle: Text(,
                      leading: CircleAvatar(
                        child: Text([0],
                            style: TextStyle(
                              fontSize: 18.0,
                              color: Colors.white,

flutter listview with sqlite db

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.


Add a Comment