Jessen Jie
Jessen Jie

Reputation: 367

How to make second or more firestore collection based on the same logged in user flutter

I want to make a new collection data that based on the same user that logged in or the same document uid.

this is the firestore Example firestore

as you can see the uid based on currently logged in user.

I have a method to insert it to database like this but if i fill the dropdowns and submit the data it just update it not increase or multiply to second, third or more.

 Future CreateEducation(MyEducation educationsss) async {
    String date1 = selectedMonthStart! + " " + selectedYearStart.toString();
    print(date1);
    // DateFormat datestart = DateFormat.yMMMM(date1);
    // print(datestart);
    String date2 = selectedMonthEnd! + " " + selectedYearEnd.toString();
    print(date2);
    final uid = FirebaseAuth.instance.currentUser!.uid;
    final docEducation = FirebaseFirestore.instance
        .collection("education")
        .doc(FirebaseAuth.instance.currentUser!.uid);

    final userEducation = MyEducation(
      uid: uid,
      universityName: collegeAheadController.text,
      universityImage: imageuniversitycollege,
      major: SelectedMajor.toString(),
      degree: SelectedDegree.toString(),
      dateEnd: date1,
      dateStart: date2,
    );
    final json = userEducation.toJson();

    await docEducation.set(json);
  }

i want to create multiple of this [only 1 per docs1

can't it be done with the same docs id? mayble alternative using uid inside the collection?

Upvotes: 0

Views: 134

Answers (1)

Janam Maharjan
Janam Maharjan

Reputation: 106

you should go for subcollections something like as follows :

     final docEducation = FirebaseFirestore.instance
        .collection("education")
        .doc(FirebaseAuth.instance.currentUser!.uid)
        .collection("educations")
        .doc();

Upvotes: 1

Related Questions