FirebaseFirestore class
A Cloud Firestore database, and the entry point for every Firestore operation.
There is one instance per FirebaseApp and database. Get it with instanceFor; from there, collection and doc give references to read, write and listen to, collectionGroup queries every collection with a given id, and batch and runTransaction group writes atomically.
The instance keeps a local cache. It is in memory unless you pass an openDatabase function to instanceFor
(SQLite on the VM, SQLite in WebAssembly on the web; see the offline persistence guide). Reads and listeners work
offline from that cache, and writes made offline are applied to the cache at once and sent when the client is
back online.
On the Dart VM the instance holds network connections and timers, so a command line program does not exit while it is alive: terminate it when you are done.
final FirebaseApp app = FirebaseApp.withOptions(
const FirebaseOptions(
apiKey: 'my-api-key',
appId: '1:1234567890:web:0123456789abcdef',
projectId: 'my-project',
),
);
final FirebaseFirestore firestore = FirebaseFirestore.instanceFor(app: app);
await firestore.doc('cities/SF').set({'name': 'San Francisco'});
final DocumentSnapshot<Map<String, Object?>> sf = await firestore
.doc('cities/SF')
.get();
print(sf.data());
await firestore.terminate();
Subclassing Note: Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.
Properties
- app → FirebaseApp
-
The FirebaseApp this instance belongs to.
no setter
- databaseId → String
-
The id of the database inside the project:
(default)unless another one was passed to instanceFor. The same name as FlutterFire'sFirebaseFirestore.databaseIdand thedatabaseargument of instanceFor.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- persistentCacheIndexManager → PersistentCacheIndexManager?
-
The PersistentCacheIndexManager of this instance, or null when it uses a memory cache.
no setter
- projectId → String
-
The id of the Google Cloud project this instance talks to, from the
projectIdof the app's options.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- settings ↔ Settings
-
The settings of this instance (the defaults of Settings when instanceFor got none and nothing set them),
for example to check whether its cache is persistent (Settings.persistenceEnabled, Settings.cacheSettings).
getter/setter pair
Methods
-
batch(
) → WriteBatch - Returns a new WriteBatch, which applies several writes as a single atomic operation.
-
clearPersistence(
) → Future< void> - Clears the persistent storage of this instance: the local SQLite database with the cached documents and the pending writes. Does nothing when the instance uses memory persistence.
-
collection(
String collectionPath) → CollectionReference< Map< String, Object?> > -
Returns a CollectionReference to the collection at
collectionPath. -
collectionGroup(
String collectionId) → Query< Map< String, Object?> > -
Returns a Query over the documents of every collection or subcollection in the database whose id is
collectionId. -
disableNetwork(
) → Future< void> - Disables network access for this instance. While the network is disabled, any snapshot listeners or get() calls will return results from cache, and any write operations will be queued until network usage is re-enabled via a call to enableNetwork.
-
doc(
String documentPath) → DocumentReference< Map< String, Object?> > -
Returns a DocumentReference to the document at
documentPath. -
documentSnapshotsFromJson<
T> (Map< String, Object?> json, {bool includeMetadataChanges = false, ListenSource source = ListenSource.defaultSource, FirestoreDataConverter<T> ? converter}) → Stream<DocumentSnapshot< T> > - Listens to the document of a DocumentSnapshot.toJson result, starting from that snapshot.
-
enableNetwork(
) → Future< void> - Re-enables network usage for this instance after a prior call to disableNetwork.
-
getPendingWriteCount(
) → Future< int> - Returns the number of write batches of the signed-in user that the backend has not acknowledged or rejected yet.
-
loadBundle(
List< int> bundleData) → LoadBundleTask - Loads a Firestore bundle into the local cache.
-
loadBundleStream(
Stream< List< bundleData) → LoadBundleTaskint> > - Loads a Firestore bundle into the local cache.
-
namedQuery(
String name) → Future< Query< Map< ?>String, Object?> > -
Reads a Firestore Query from local cache, identified by the given
name. -
namedQueryGet(
String name, {GetOptions options = const GetOptions()}) → Future< QuerySnapshot< Map< >String, Object?> > -
Runs the named query
nameof a loaded bundle (see namedQuery) and returns its results: FlutterFire'snamedQueryGet. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
pendingWriteCount(
) → Future< int> - Returns the number of write batches of the signed-in user that the backend has not acknowledged or rejected yet.
-
pendingWriteCounts(
) → Stream< int> - A stream of getPendingWriteCount: it emits the count when it is listened to, and then a new count after the queue of pending writes changed (a write, an acknowledgement, a rejection, a change of the signed-in user).
-
querySnapshotsFromJson<
T> (Map< String, Object?> json, {bool includeMetadataChanges = false, ListenSource source = ListenSource.defaultSource, FirestoreDataConverter<T> ? converter}) → Stream<QuerySnapshot< T> > - Listens to the query of a QuerySnapshot.toJson result, resuming from that snapshot.
-
runBatch(
BatchCallback batchFunction) → Future< void> -
Runs
batchFunctionon a new WriteBatch and then commits the writes it added as a single atomic unit. -
runTransaction<
T> (TransactionHandler< T> updateFunction, {Duration timeout = const Duration(seconds: 30), int maxAttempts = 5, bool readOnly = false, Timestamp? readTime}) → Future<T> -
Runs
updateFunctionin a transaction and commits its writes atomically; completes with the value it returns. -
setIndexConfigurationFromJSON(
String json) → Future< void> - The old spelling of setIndexConfigurationFromJson.
-
setIndexConfigurationFromJson(
String json) → Future< void> -
Configures the persistent cache indexes of this instance from an index definition in the JSON format of the
Firebase CLI (
firestore.indexes.json, see https://firebase.google.com/docs/reference/firestore/indexes/#json_format). ThequeryScopeof an index and thefieldOverridesare ignored. The indexes replace the ones that were configured before; an index that is configured again keeps its entries. -
snapshotsInSync(
) → Stream< void> - A stream that emits whenever all the snapshot listeners of this instance are in sync with each other, which means every listener received the snapshot that reflects the same state of the local cache and of the backend.
-
terminate(
) → Future< void> - Terminates this FirebaseFirestore instance, releasing all the resources it holds.
-
toString(
) → String -
A string representation of this object.
inherited
-
useFirestoreEmulator(
String host, int port, {bool sslEnabled = false}) → void -
Points this instance at the Firestore emulator on
hostandport, as in FlutterFire: allowed until the instance is first used, like the settings setter. The same as settingsettings.withEmulator(host, port), withsslEnabledoff by default. Unlike FlutterFire,localhostis not mapped to10.0.2.2on the Android emulator: pass10.0.2.2yourself there. -
waitForPendingWrites(
) → Future< void> - Returns a future that completes when all the writes that are pending at the time of the call were acknowledged (or rejected) by the backend. Writes that are made later are not waited for.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → FirebaseFirestore
-
The instance of the default FirebaseApp and the default database.
no setter
Static Methods
-
instanceFor(
{required FirebaseApp app, String databaseId = DatabaseId.defaultDatabaseId, OpenDatabase? openDatabase, Settings? settings}) → FirebaseFirestore -
Gets the instance for
appanddatabaseId, creating it on the first call. Later calls return the same instance until it is terminated. -
setLoggingEnabled(
bool enabled) → void - Globally enables / disables Firestore logging for the SDK.
-
setLogLevel(
LogLevel level) → void -
Sets the verbosity of the Cloud Firestore logs: from LogLevel.verbose
(everything) and LogLevel.debug through LogLevel.warn (the default)
to LogLevel.error (errors only) and LogLevel.silent (nothing). The
levels of
setLogLevelof the JS SDK, withoutinfo.