user10082972
user10082972

Reputation:

'Invalid document reference. Document references must have an even number of segments' in flutter

I am using firestore with flutter. I am constantly getting this error and not able to solve it despite reading all available reference.Can Someone please help me solve the problem. My Code -

String uid = '+919101006470';
final snapShot = await db.collection("users").document(uid).get();

The error is

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but users has 1, null)

Upvotes: 2

Views: 3574

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600071

The cause of the error is quite simple, you have a path with an uneven number of segments, which means it points to a collection and not to a document.

The easiest way to troubleshoot is to print the path of the document reference before calling get() on it:

final ref = db.collection("users").document(uid).get();
print (ref.path);
final snapShot = await ref;

As Doug commented, on the code you gave it seems most likely that uid doesn't have a value, but printing the path will show you which segment of your path is empty/missing.

Upvotes: 5

Related Questions