cloud_firestore_dart 0.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 66

cloud_firestore_dart #

Dart implementation of cloud_firestore using cloud_firestore_vm

Getting Started #

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

[0.0.1] - TODO: Add release date. #

  • TODO: Describe initial release.

example/lib/main.dart

// Copyright 2017, the Chromium project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore_dart/cloud_firestore_dart.dart';
import 'package:firebase_auth_dart/firebase_auth_dart.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:firebase_core_dart/firebase_core_dart.dart';
import 'package:firebase_core_vm/firebase_core_vm.dart' show isDesktop;
import 'package:flutter/material.dart';

Future<void> main() async {
  if (isDesktop) {
    await FirebaseCoreDart.register(
      options: const FirebaseOptions(
        apiKey: 'AIzaSyBQgB5s3n8WvyCOxhCws-RVf3C-6VnGg0A',
        databaseURL: 'https://flutter-sdk.firebaseio.com',
        projectID: 'flutter-sdk',
        storageBucket: 'flutter-sdk.appspot.com',
        gcmSenderID: '233259864964',
        googleAppID:
            '1:233259864964:macos:0bdc69800dd31cde15627229f39a6379865e8be1',
      ),
    );
    await FirebaseAuthDart.register();
    await FirestoreDart.register();
  }

  runApp(
    MaterialApp(
      title: 'Firestore Example',
      home: MyHomePage(firestore: Firestore.instance),
    ),
  );
}

class MessageList extends StatelessWidget {
  const MessageList({Key key, @required this.firestore}) : super(key: key);

  final Firestore firestore;

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: firestore
          .collection('messages')
          .orderBy('created_at', descending: true)
          .snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (!snapshot.hasData) {
          return const Text('Loading...');
        }

        final int messageCount = snapshot.data.documents.length;
        return ListView.builder(
          itemCount: messageCount,
          itemBuilder: (_, int index) {
            final DocumentSnapshot document = snapshot.data.documents[index];
            final dynamic message = document['message'];
            return ListTile(
              trailing: IconButton(
                onPressed: () => document.reference.delete(),
                icon: Icon(Icons.delete),
              ),
              title: Text(
                message != null ? message.toString() : '<No message retrieved>',
              ),
              subtitle: Text('Message ${index + 1} of $messageCount'),
            );
          },
        );
      },
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key key, @required this.firestore}) : super(key: key);

  final Firestore firestore;

  CollectionReference get messages => firestore.collection('messages');

  Future<void> _addMessage() async {
    await messages.add(<String, dynamic>{
      'message': 'Hello world!',
      'created_at': FieldValue.serverTimestamp(),
    });
  }

  Future<void> _runTransaction() async {
    await firestore.runTransaction((Transaction transaction) async {
      final QuerySnapshot allDocs =
          await firestore.collection('messages').getDocuments();
      final List<DocumentSnapshot> toBeRetrieved =
          allDocs.documents.sublist(allDocs.documents.length ~/ 2);
      final List<DocumentSnapshot> toBeDeleted =
          allDocs.documents.sublist(0, allDocs.documents.length ~/ 2);
      await Future.forEach(toBeDeleted, (DocumentSnapshot snapshot) async {
        await transaction.delete(snapshot.reference);
      });

      await Future.forEach(toBeRetrieved, (DocumentSnapshot snapshot) async {
        await transaction.update(snapshot.reference, <String, dynamic>{
          'message': 'Updated from Transaction',
          'created_at': FieldValue.serverTimestamp()
        });
      });
    });

    await Future.forEach(List<int>.generate(2, (int index) => index),
        (int item) async {
      await firestore.runTransaction((Transaction transaction) async {
        await Future.forEach(List<int>.generate(10, (int index) => index),
            (int item) async {
          await transaction.set(
              firestore.collection('messages').document(), <String, dynamic>{
            'message': 'Created from Transaction $item',
            'created_at': FieldValue.serverTimestamp()
          });
        });
      });
    });
  }

  Future<void> _runBatchWrite() async {
    final WriteBatch batchWrite = firestore.batch();
    final QuerySnapshot querySnapshot = await firestore
        .collection('messages')
        .orderBy('created_at')
        .limit(12)
        .getDocuments();
    querySnapshot.documents
        .sublist(0, querySnapshot.documents.length - 3)
        .forEach((DocumentSnapshot doc) {
      batchWrite.updateData(doc.reference, <String, dynamic>{
        'message': 'Batched message',
        'created_at': FieldValue.serverTimestamp()
      });
    });

    batchWrite
      ..setData(
        firestore.collection('messages').document(),
        <String, dynamic>{
          'message': 'Batched message created',
          'created_at': FieldValue.serverTimestamp()
        },
      )
      ..delete(
          querySnapshot.documents[querySnapshot.documents.length - 2].reference)
      ..delete(querySnapshot.documents.last.reference);
    await batchWrite.commit();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Firestore Example'),
        actions: <Widget>[
          FlatButton(
            onPressed: _runTransaction,
            child: const Text('Run Transaction'),
          ),
          FlatButton(
            onPressed: _runBatchWrite,
            child: const Text('Batch Write'),
          )
        ],
      ),
      body: MessageList(firestore: firestore),
      floatingActionButton: FloatingActionButton(
        onPressed: _addMessage,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  cloud_firestore_dart: ^0.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:cloud_firestore_dart/cloud_firestore_dart.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
39
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform android

Because:

  • package:cloud_firestore_dart/cloud_firestore_dart.dart that imports:
  • package:cloud_firestore_dart/src/util/database.dart that imports:
  • package:moor_ffi/database.dart that declares support for platforms:

Package does not support Flutter platform ios

Because:

  • package:cloud_firestore_dart/cloud_firestore_dart.dart that imports:
  • package:cloud_firestore_dart/src/util/database.dart that imports:
  • package:moor_ffi/database.dart that declares support for platforms:

Package does not support Flutter platform linux

Because:

  • package:cloud_firestore_dart/cloud_firestore_dart.dart that imports:
  • package:firebase_core/firebase_core.dart that declares support for platforms: android, ios, macos, web

Package does not support Flutter platform macos

Because:

  • package:cloud_firestore_dart/cloud_firestore_dart.dart that imports:
  • package:cloud_firestore_dart/src/util/database.dart that imports:
  • package:moor_ffi/database.dart that declares support for platforms:

Package does not support Flutter platform web

Because:

  • package:cloud_firestore_dart/cloud_firestore_dart.dart that imports:
  • package:cloud_firestore_dart/src/util/database.dart that imports:
  • package:sqflite/sqflite.dart that declares support for platforms: android, ios, macos

Package does not support Flutter platform windows

Because:

  • package:cloud_firestore_dart/cloud_firestore_dart.dart that imports:
  • package:firebase_core/firebase_core.dart that declares support for platforms: android, ios, macos, web

Package not compatible with SDK dart

Because:

  • cloud_firestore_dart that is a package requiring null.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (moor_ffi).

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
cloud_firestore_platform_interface ^1.1.0 1.1.2
cloud_firestore_vm ^0.0.5 0.0.7
cloud_firestore_web ^0.1.1+2 0.1.1+2
firebase_core ^0.4.4+3 0.4.5
firebase_core_vm ^0.0.4 0.0.4
flutter 0.0.0
meta ^1.1.8 1.1.8 1.2.2
moor_ffi ^0.4.0 0.4.0 0.7.0
path_provider ^1.6.5 1.6.11
sqflite ^1.3.0 1.3.1
Transitive dependencies
_firebase_database_collection_vm 0.0.2
_firebase_internal_vm 0.0.2
async 2.4.2
charcode 1.1.3
collection 1.14.12 1.14.13
convert 2.1.1
crypto 2.1.5
ffi 0.1.3
file 5.2.1
firebase 7.3.0
firebase_core_platform_interface 1.0.4
firebase_core_web 0.1.1+2
fixnum 0.10.11
flutter_web_plugins 0.0.0
googleapis_auth 0.2.12
grpc 2.2.0
http 0.12.1
http2 1.0.0
http_parser 3.1.4
intl 0.16.1
js 0.6.2
matcher 0.12.8
moor 2.4.2 3.2.0
path 1.7.0
path_provider_linux 0.0.1+2
path_provider_macos 0.0.4+3
path_provider_platform_interface 1.0.2
pedantic 1.9.0 1.9.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
protobuf 1.0.1
quiver 2.1.3
rxdart 0.23.1 0.24.1
semaphore 0.1.4
sky_engine 0.0.99
source_span 1.7.0
sqflite_common 1.0.2+1
stack_trace 1.9.5
string_scanner 1.0.5
synchronized 2.2.0+1
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xdg_directories 0.1.0