Juan Casas
Juan Casas

Reputation: 122

creating a class from a stream

I have a button to apply for a job. If I press the button and I dont already have applied to the job, the code should create a new job application, then open the page to see that application. [ this is working perfect ]

If I press the button and I already have a job application for that job, the code should open the page to see that application. [ this is not working ]

sounds pretty simple, yet I cant get it to work!! :( :(

            onPressed: () async {
      final job = _job;
      if (job == null) {
        return;
      }


      // META DATA FOR CREATING JOB APPLICATION start
      final userData = await FirebaseFirestore.instance
          .collection('user')
          .doc(currentUser.id)
          .get();
      var jobUserData = [
        {
          "userFirstName": job.jobCreatorFirstName,
          "userId": _jobCreatorId,
          "userRole": "jobCreator"
        },
        {
          "userFirstName":
              userData[userFirstNameColumn],
          "userId": currentUser.id,
          "userRole": "jobApplicator",
        },
      ];

      var jobApplicationId = await _jobsService
          .getJobApplicationIdIfPossible(
              job.documentId, currentUser.id);

      var jobApplicationData = await _jobsService
          .getJobApplicationObject(jobApplicationId);

      // META DATA FOR CREATING JOB APPLICATION end


      if (jobApplicationId == '') {
        // THIS WORKS PERECT
        print('creating job application');

        var newJobApplication =
            await _jobsService.createNewJobApplication(
          jobCreatorId: _jobCreatorId,
          jobUsersData: jobUserData,
          jobApplicatorId: currentUser.id,
          jobId: job.documentId,
          jobApplicationDescription:
              _jobDescriptionController.text,
        );
        Navigator.of(context).pushNamed(
          myJobApplicationsRoute,
          arguments: newJobApplication,
        );
      } else {
       // THIS DOES NOT WORK AT ALL
        var jobApplication2 = await _jobsService
            .jobApplication
            .get()
            .then((QuerySnapshot querySnapshot) {
          querySnapshot.docs.forEach((doc) {
            CloudJobApplication(
              documentId: jobApplicationId,
              jobApplicatorId: currentUser.id,
              jobCreatorId: _jobCreatorId,
              jobApplicationState: jobApplicationData[
                  "Job application state"],
              jobApplicationSubState:
                  jobApplicationData[
          "Job application substate"],
              jobApplicationDescription:
                  jobApplicationData["Job description"],
              jobUserData:
                  jobApplicationData["Job description"],
              jobId: '',
            );
          });
        });

        Navigator.of(context).pushNamed(
          myJobApplicationsRoute,
          arguments: jobApplication2,
        );

        print('job application already present');
      }
                },
              
              

i think the issue is the 'for each part'

this is the error I get

E/flutter ( 3525): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'List<dynamic>'
E/flutter ( 3525): #0      _CreateUpdateJobViewState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure>
package:ijob_clone_app/…/jobs/create_update_job_view.dart:591
E/flutter ( 3525): #1      List.forEach (dart:core-patch/growable_array.dart:416:8)
E/flutter ( 3525): #2      _CreateUpdateJobViewState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>
package:ijob_clone_app/…/jobs/create_update_job_view.dart:578
E/flutter ( 3525): <asynchronous suspension>
E/flutter ( 3525): #3      _CreateUpdateJobViewState.build.<anonymous closure>.<anonymous closure>
package:ijob_clone_app/…/jobs/create_update_job_view.dart:574
E/flutter ( 3525): <asynchronous suspension>
E/flutter ( 3525):

The second issue is an optimization thing. In the code above I have 3 calls to the server to check and/or create a job application.

Here are the functions that I am ussing::

 Future getJobApplicationIdIfPossible(
      String jobId, String jobApplicatorId) async {
    var docId = '';
    await jobApplication
        .where(jobIdColumn, isEqualTo: jobId)
        .where(jobApplicatorIdColumn, isEqualTo: jobApplicatorId)
        .get()
        .then((value) {
      value.docs.forEach((element) {
        docId = element.id;
      });
    });
    return docId;
  }

  Future getJobApplicationObject(String jobApplicationDocumentId) async {
    var data;
    final docRef = await jobApplication.doc(jobApplicationDocumentId);
    await docRef.get().then(
      (DocumentSnapshot doc) {
        data = doc.data() as Map<String, dynamic>;

        // ...
      },
      onError: (e) => print("Error getting document: $e"),
    );
    return data;
  }

Is there any way to make that a little better? I feel like maybe that could be made into two calls maybe?

Upvotes: 0

Views: 59

Answers (1)

Juan Casas
Juan Casas

Reputation: 122

I got it to work::

       var jobApplication2 = CloudJobApplication(
                              documentId: jobApplicationId,
                              jobApplicatorId: currentUser.id,
                              jobCreatorId: _jobCreatorId,
                              jobApplicationState: jobApplicationData[
                                  "Job application state"],
                              jobApplicationSubState: jobApplicationData[
                                  "Job application substate"],
                              jobApplicationDescription:
                                  jobApplicationData["Job description"],
                              jobUserData:
                                  jobApplicationData["Job users data"],
                              jobId: '',
                            );

                            Navigator.of(context).pushNamed(
                              myJobApplicationsRoute,
                              arguments: jobApplication2,
                            );

Upvotes: 0

Related Questions