Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cloud_firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.12.8

* Updated how document ids are generated to more closely match native implementations.

## 0.12.7+1

* Update google-services Android gradle plugin to 4.3.0 in documentation and examples.
Expand Down
2 changes: 1 addition & 1 deletion packages/cloud_firestore/lib/cloud_firestore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

import 'src/utils/push_id_generator.dart';
import 'src/utils/auto_id_generator.dart';

part 'src/blob.dart';
part 'src/collection_reference.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/cloud_firestore/lib/src/collection_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CollectionReference extends Query {
DocumentReference document([String path]) {
List<String> childPath;
if (path == null) {
final String key = PushIdGenerator.generatePushChildName();
final String key = AutoIdGenerator.autoId();
childPath = List<String>.from(_pathComponents)..add(key);
} else {
childPath = List<String>.from(_pathComponents)..addAll(path.split(('/')));
Expand Down
34 changes: 34 additions & 0 deletions packages/cloud_firestore/lib/src/utils/auto_id_generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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:math';

/// Utility class for generating Firebase child node keys.
///
/// Since the Flutter plugin API is asynchronous, there's no way for us
/// to use the native SDK to generate the node key synchronously and we
/// have to do it ourselves if we want to be able to reference the
/// newly-created node synchronously.
///
/// This code is based largely on the Android implementation and ported to Dart.

class AutoIdGenerator {
static const int _AUTO_ID_LENGTH = 20;

static const String _AUTO_ID_ALPHABET =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

static final Random _random = Random();

static String autoId() {
final StringBuffer stringBuffer = StringBuffer();
final int maxRandom = _AUTO_ID_ALPHABET.length;

for (int i = 0; i < _AUTO_ID_LENGTH; ++i) {
stringBuffer.write(_AUTO_ID_ALPHABET[_random.nextInt(maxRandom)]);
}

return stringBuffer.toString();
}
}
63 changes: 0 additions & 63 deletions packages/cloud_firestore/lib/src/utils/push_id_generator.dart

This file was deleted.