0

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),
      ),
    );
  }
}
  • Have you recently stored the todo instance in firestore? If not there is no firestore ID yet - you can assign your own or have one automatically generated. If the todo instance is stored but you don't have the ID, you may need to use a query on some other properties to find the instance again. – Chris Jul 21 '22 at 19:17
  • @Chris the instance is stored in firestore and has an automatically generated id. I guess I could query on all the properties to find a particular instance. However, I don't know if it's a good practice if the document has so many fields. – user18347947 Jul 21 '22 at 20:47

2 Answers2

0

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>;
    // ...
  },
  onError: (e) => print("Error getting document: $e"),
);

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

Chris
  • 846
  • 6
  • 16
0

just use

var todoId = await db.collection("todoCollection").doc(todoId).id;
Dean James
  • 2,491
  • 4
  • 21
  • 29