| // Copyright 2017, the Flutter 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. |
| |
| part of firebase_database; |
| |
| /// DatabaseReference represents a particular location in your Firebase |
| /// Database and can be used for reading or writing data to that location. |
| /// |
| /// This class is the starting point for all Firebase Database operations. |
| /// After you’ve obtained your first DatabaseReference via |
| /// `FirebaseDatabase.reference()`, you can use it to read data |
| /// (ie. `onChildAdded`), write data (ie. `setValue`), and to create new |
| /// `DatabaseReference`s (ie. `child`). |
| class DatabaseReference extends Query { |
| DatabaseReference._(FirebaseDatabase database, List<String> pathComponents) |
| : super._(database, pathComponents); |
| |
| /// Gets a DatabaseReference for the location at the specified relative |
| /// path. The relative path can either be a simple child key (e.g. ‘fred’) or |
| /// a deeper slash-separated path (e.g. ‘fred/name/first’). |
| DatabaseReference child(String path) { |
| return new DatabaseReference._( |
| _database, |
| (new List<String>.from(_pathComponents)..addAll(path.split('/'))) |
| ); |
| } |
| |
| /// Gets a DatabaseReference for the parent location. If this instance |
| /// refers to the root of your Firebase Database, it has no parent, and |
| /// therefore parent() will return null. |
| DatabaseReference parent() { |
| if (_pathComponents.isEmpty) |
| return null; |
| return new DatabaseReference._( |
| _database, |
| (new List<String>.from(_pathComponents)..removeLast()) |
| ); |
| } |
| |
| /// Gets a FIRDatabaseReference for the root location. |
| DatabaseReference root() { |
| return new DatabaseReference._(_database, <String>[]); |
| } |
| |
| /// Gets the last token in a Firebase Database location (e.g. ‘fred’ in |
| /// https://SampleChat.firebaseIO-demo.com/users/fred) |
| String get key => _pathComponents.last; |
| |
| /// Generates a new child location using a unique key and returns a |
| /// DatabaseReference to it. This is useful when the children of a Firebase |
| /// Database location represent a list of items. |
| /// |
| /// The unique key generated by childByAutoId: is prefixed with a |
| /// client-generated timestamp so that the resulting list will be |
| /// chronologically-sorted. |
| DatabaseReference push() { |
| String key = PushIdGenerator.generatePushChildName(); |
| List<String> childPath = new List<String>.from(_pathComponents)..add(key); |
| return new DatabaseReference._(_database, childPath); |
| } |
| |
| /// Write data to this Firebase Database location. |
| /// |
| /// This will overwrite any data at this location and all child locations. |
| /// |
| /// Data types that are allowed are String, boolean, int, double, Map, List. |
| /// |
| /// The effect of the write will be visible immediately and the corresponding |
| /// events will be triggered. Synchronization of the data to the Firebase |
| /// Database servers will also be started. |
| /// |
| /// Passing null for the new value means all data at this location or any |
| /// child location will be deleted. |
| Future<Null> set(dynamic value) async { |
| await _database._channel.invokeMethod( |
| 'DatabaseReference#set', |
| { 'path': path, 'value': value }, |
| ); |
| } |
| } |
| class ServerValue { |
| static const timestamp = const {'.sv' : 'timestamp'}; |
| } |