skip to Main Content

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),
      ),
    );
  }
}

2

Answers


  1. 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.

    Login or Signup to reply.
  2. just use

    var todoId = await db.collection("todoCollection").doc(todoId).id;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search