Home > Back-end >  Get the id of a firestore document in flutter
Get the id of a firestore document in flutter

Time:07-22

after following the flutter documentation code to send data to a new screen, I want to retrieve the firestore id of the todo instance in the detail screen. is there a way to do that? this is the detail screen:

  const DetailScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final todo = ModalRoute.of(context)!.settings.arguments as Todo;

    // Use the Todo to create the UI.
    return Scaffold(
      appBar: AppBar(
        title: Text(todo.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Text(todo.description),
      ),
    );
  }
}

CodePudding user response:

When storing or reading the item(s) from the database you should keep the handle of the document ID. Then, when you move to the detail page you can do a firebase lookup on the document ID of the todo instance like so:

final docRef = db.collection("myCollection").doc(todoId);
docRef.get().then(
  (DocumentSnapshot doc) {
    final data = doc.data() as Map<String, dynamic>;
    // ...
  },
  one rror: (e) => print("Error getting document: $e"),
);

This answer covers sending data between flutter screens. See the Firebase docs here.

CodePudding user response:

just use

var todoId = await db.collection("todoCollection).doc(todoId).id;
  • Related