Git Product home page Git Product logo

rhedgpeth / flutter-sqlite Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 4.0 1.19 MB

This repository contains a simple Flutter application that manages contact information using SQLite (via SQFLite).

License: MIT License

Kotlin 0.75% Ruby 8.07% Swift 2.41% Objective-C 0.23% Dart 88.54%
flutter flutter-apps flutter-examples flutter-sqlite flutter-sqflite flutter-sql bitrise bitrise-flutter cicd quickstart

flutter-sqlite's Introduction

Flutter + SQLite

Build status License (MIT)

This repository contains a simple Flutter application that manages contact information using SQLite (via SQFLite).

Requirements

To run this application be sure to check out the Flutter documentation for getting started.

Key takeaways

To use SQLite within a Flutter application you're going to need to add a couple dependencies to the pubspec.yaml file.

Note: The Dart ecosystem uses packages to manage shared software such as libraries and tools. To get Dart packages, you use the pub package manager. Find more information on Dart package management here.

More specifically, the sqflite and path_provider packages are required.

dependencies:
  flutter:
    sdk: flutter
  sqflite: any
  path_provider: any

From there a SQLite database (and tables) can be created and used within the application.

For this applicaiton I have put all of the SQFLite usages within database_helper.dart.

import 'dart:async';
import 'dart:io' as io;
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:rolodex/models/base_model.dart';
import 'package:sqflite/sqflite.dart';

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

  factory DatabaseHelper() => _instance;

  static Database? _db;

  Future<Database> get db async {
    _db ??= await init();
    return _db!;
  }

  DatabaseHelper.internal();

  Future<Database> init() async {
      io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
      String path = join(documentsDirectory.path, "main.db");
      return openDatabase(path, version: 1, onCreate: onCreate);
  }

  void onCreate(Database db, int version) async =>
      await db.execute('CREATE TABLE contacts (id INTEGER PRIMARY KEY NOT NULL, firstName STRING, lastName STRING, phone STRING, email STRING)');

  Future<List<Map<String, dynamic>>> query(String table) async => (await db).query(table);

  Future<int> insert(String table, BaseModel model) async => (await db).insert(table, model.toMap());

  Future<int> update(String table, BaseModel model) async => (await db).update(table, model.toMap(), where: 'id = ?', whereArgs: [model.id]);

  Future<int> delete(String table, BaseModel model) async => (await db).delete(table, where: 'id = ?', whereArgs: [model.id]);
}

Questions / Feedback

If you have any questions or feedback for this sample please feel free to submit an issue here or email me at [email protected].

Happy coding!

flutter-sqlite's People

Contributors

rhedgpeth avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.