Skip to content

[firestore_database]: Cannot store DocumentReference as map field key #17996

@paulchirita

Description

@paulchirita

Is there an existing issue for this?

  • I have searched the existing issues.

Which plugins are affected?

Database

Which platforms are affected?

iOS, macOS, Web, Windows, Android

Description

Actual behavior

When using a DocumentReference as a map field key and attempting to write that map field to a Firestore document, the SDK throws a type cast error:
type '_JsonDocumentReference' is not a subtype of type 'String' in type cast

Expected behavior

Since Firestore natively supports DocumentReference as map keys (this can be verified by manually adding a reference-type map key via the Firebase Console UI), the FlutterFire SDK should serialize DocumentReference keys the same way it handles DocumentReference values.

Reproducing the issue

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  final firestore = FirebaseFirestore.instance;
  final docRef = firestore.doc('testCollection/testDoc');
  final Map<DocumentReference, double> doublePerRef = {
    docRef: 100.0,
  };
  try {
    await firestore.doc('testCollection/testDoc').set({
      'doublePerRef': doublePerRef,
    });
    print('Success');
  } catch (e) {
    print('Error: $e');
    // Error: type '_JsonDocumentReference' is not a subtype of type 'String' in type cast
  }
}

Firebase Core version

3.15.2

Flutter Version

3.35.7

Relevant Log Output

Flutter dependencies

No response

Additional context and comments

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions