createUniqueDoc method

Future<String?> createUniqueDoc(
  1. String path,
  2. Map<String, dynamic> data
)

Creates a new unique document at the specified path in the RTDB.

  • path: The database path where the document should be created.
  • data: The data to write as a Map<String, dynamic>.

Returns the unique document ID on success, or null if an error occurs.

Implementation

Future<String?> createUniqueDoc(String path, Map<String, dynamic> data) async {
  _startRateLimitResetTimer();
  final authToken = await _getAuthToken();
  if (authToken == null) return null;

  final url = Uri.parse('$_baseUrl$path.json?auth=$authToken');

  try {
    _currentHttpConnections++;
    _connectionOrder.add(path);
    final response = await _client.post(
      url,
      headers: _jsonHeaders,
      body: json.encode(data),
    );
    _currentHttpConnections--;

    if (response.statusCode == 200) {
      final responseBody = json.decode(response.body);
      debugPrint('Document created with ID: ${responseBody['name']}');
      return responseBody['name'];
    } else {
      debugPrint('Error creating document. Status code: ${response.statusCode}');
      return null;
    }
  } catch (e) {
    _currentHttpConnections--;
    debugPrint('Error creating unique document: $e');
    return null;
  }
}