Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 6ff0789

Browse files
KevinTheGraymithun-mondal
authored andcommitted
[cloud_firestore] Update autoId generation to match native implementation more closely (flutter#1907)
1 parent b653055 commit 6ff0789

5 files changed

Lines changed: 40 additions & 65 deletions

File tree

packages/cloud_firestore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.12.8
2+
3+
* Updated how document ids are generated to more closely match native implementations.
4+
15
## 0.12.7+1
26

37
* Update google-services Android gradle plugin to 4.3.0 in documentation and examples.

packages/cloud_firestore/lib/cloud_firestore.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;
1515
import 'package:flutter/services.dart';
1616
import 'package:meta/meta.dart';
1717

18-
import 'src/utils/push_id_generator.dart';
18+
import 'src/utils/auto_id_generator.dart';
1919

2020
part 'src/blob.dart';
2121
part 'src/collection_reference.dart';

packages/cloud_firestore/lib/src/collection_reference.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class CollectionReference extends Query {
4040
DocumentReference document([String path]) {
4141
List<String> childPath;
4242
if (path == null) {
43-
final String key = PushIdGenerator.generatePushChildName();
43+
final String key = AutoIdGenerator.autoId();
4444
childPath = List<String>.from(_pathComponents)..add(key);
4545
} else {
4646
childPath = List<String>.from(_pathComponents)..addAll(path.split(('/')));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2017, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:math';
6+
7+
/// Utility class for generating Firebase child node keys.
8+
///
9+
/// Since the Flutter plugin API is asynchronous, there's no way for us
10+
/// to use the native SDK to generate the node key synchronously and we
11+
/// have to do it ourselves if we want to be able to reference the
12+
/// newly-created node synchronously.
13+
///
14+
/// This code is based largely on the Android implementation and ported to Dart.
15+
16+
class AutoIdGenerator {
17+
static const int _AUTO_ID_LENGTH = 20;
18+
19+
static const String _AUTO_ID_ALPHABET =
20+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
21+
22+
static final Random _random = Random();
23+
24+
static String autoId() {
25+
final StringBuffer stringBuffer = StringBuffer();
26+
final int maxRandom = _AUTO_ID_ALPHABET.length;
27+
28+
for (int i = 0; i < _AUTO_ID_LENGTH; ++i) {
29+
stringBuffer.write(_AUTO_ID_ALPHABET[_random.nextInt(maxRandom)]);
30+
}
31+
32+
return stringBuffer.toString();
33+
}
34+
}

packages/cloud_firestore/lib/src/utils/push_id_generator.dart

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)