Skip to content

putraxor/firebase_dart_sdk

 
 

Repository files navigation

Firebase Dart SDK

Unofficial Firebase Dart SDK

Firestore Usage

Set dependencies in pubspec.yaml

dependencies:
  path: ^1.4.1
  sqlite:
    git: https://github.com/long1eu/dart-sqlite
  firebase_common:
    git:
      url: https://github.com/putraxor/firebase_dart_sdk
      path: firebase_common
  firebase_firestore:
    git:
      url: https://github.com/putraxor/firebase_dart_sdk
      path: firebase_firestore 

Create local database for firestore local_db.dart

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

import 'package:firebase_firestore/src/firebase/firestore/util/database.dart';
import 'package:sqlite/sqlite.dart' as sql;

class LocalDb extends Database {
  sql.Database database;
  File path;

  bool renamePath = true;

  LocalDb._(this.database, this.path);

  static Future<LocalDb> create(String name,
      {int version,
      OnConfigure onConfigure,
      OnCreate onCreate,
      OnVersionChange onUpgrade,
      OnVersionChange onDowngrade,
      OnOpen onOpen}) async {
    version ??= 1;

    final File path = File('${Directory.current.path}/build/test/$name');
    final bool callOnCreate = !path.existsSync();
    path.createSync(recursive: true);

    final sql.Database database = sql.Database(path.path);
    final LocalDb mock = LocalDb._(database, path);

    await onConfigure?.call(mock);
    if (callOnCreate) {
      await onCreate?.call(mock, version);
      await database.execute('PRAGMA user_version = $version;');
    } else {
      final List<sql.Row> row =
          await database.query('PRAGMA user_version;').toList();
      final int currentVersion = row.first.toMap().values.first;

      if (currentVersion < version) {
        await onUpgrade?.call(mock, currentVersion, version);
        await database.execute('PRAGMA user_version = $version;');
      }

      if (currentVersion > version) {
        await database.execute('PRAGMA user_version = $version;');
        await onDowngrade?.call(mock, currentVersion, version);
      }
    }

    await onOpen?.call(mock);
    return mock;
  }

  @override
  Future<int> delete(String statement, [List<dynamic> arguments]) {
    return database.execute(statement, params: arguments ?? <dynamic>[]);
  }

  @override
  Future<void> execute(String statement, [List<dynamic> arguments]) async {
    await database.execute(statement, params: arguments ?? <dynamic>[]);
  }

  @override
  Future<List<Map<String, dynamic>>> query(String statement,
      [List<void> arguments]) async {
    return database
        .query(statement, params: arguments ?? <dynamic>[])
        .toList()
        .then(
          (List<sql.Row> rows) =>
              rows.map((sql.Row row) => row.toMap()).toList(),
        );
  }

  @override
  void close() {
    database.close();
    if (renamePath) {
      path.renameSync('${path.path}_');
    }
  }
}

Minimal implementation main.dart

import 'package:firebase_firestore/firebase_firestore.dart';
import 'package:firebase_common/firebase_common.dart';
import 'local_db.dart';

main(List<String> args) async {
  Log.level = LogLevel.e;
  var options = FirebaseOptions(
    apiKey: "********",
    projectId: "********",
    storageBucket: "********.appspot.com",
    applicationId: '1:7015********:android:f3791********',
    databaseUrl: 'https://********.firebaseio.com',
    gcmSenderId: '7015********',
  );

  var app = FirebaseApp.withOptions(options, null, null, "My App");

  var firestore =
      await FirebaseFirestore.getInstance(app, openDatabase: LocalDb.create);

  var query = await firestore
      .collection('data')
      .document('dev')
      .collection('signal')
      .get();

  query.documents.forEach((doc) {
    print('\nFound document ${doc.data}');
  });
}

About

Unofficial Firebase Pure Dart SDK

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 99.6%
  • Other 0.4%