CidQu
CidQu

Reputation: 450

How to bypass field does not exist within the DocumentSnapshotPlatform

I'm receiving this error because some of my contents don't have ['ses'] in firestore. Half of the contents have this and the other half do not have it. How can I fix this error? If the content doesn't have ['ses'] in firestore database I just want it to pass, make that variable null. How can I do this?

DetailsPage(
    tag: i['timestamp'],
    imageUrl:
        i['image url'],
    category: i['name'],
    timestamp:
        i['timestamp'],
    detay:
        i['hakkinda'],
    habitat:
        i['habitat'],
    yayilis:
        i['yayilis'],
    beslenme:
        i['beslenme'],
    biyoloji:
        i['biyoloji'],
    biyoname:
        i['biyoname'],
    ses: i['ses'])));

Some of the files has "SES" like this one: ses

Some of the files don't have 'ses':

enter image description here

So Because of this code gave me an error and says "couldn't find 'ses'" and gave this error: field does not exist within the DocumentSnapshotPlatform I want to bypass this, if the file doesn't exist just skip. How can I do this?

Upvotes: 0

Views: 173

Answers (1)

CidQu
CidQu

Reputation: 450

Fixed it by using "try {} catch() {}". Here is the code.

onTap: () {
                                        try {
                                          var ses = i['ses'];
                                          Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                  builder: (context) =>
                                                      DetailsPage(
                                                          tag: i[
                                                              'timestamp'],
                                                          imageUrl: i[
                                                              'image url'],
                                                          category:
                                                              i['name'],
                                                          timestamp: i[
                                                              'timestamp'],
                                                          detay:
                                                              i['hakkinda'],
                                                          habitat:
                                                              i['habitat'],
                                                          yayilis:
                                                              i['yayilis'],
                                                          beslenme:
                                                              i['beslenme'],
                                                          biyoloji:
                                                              i['biyoloji'],
                                                          biyoname:
                                                              i['biyoname'],
                                                          ses: ses)));
                                        } catch (e) {
                                          Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                  builder: (context) =>
                                                      DetailsPage(
                                                          tag: i[
                                                              'timestamp'],
                                                          imageUrl: i[
                                                              'image url'],
                                                          category:
                                                              i['name'],
                                                          timestamp: i[
                                                              'timestamp'],
                                                          detay:
                                                              i['hakkinda'],
                                                          habitat:
                                                              i['habitat'],
                                                          yayilis:
                                                              i['yayilis'],
                                                          beslenme:
                                                              i['beslenme'],
                                                          biyoloji:
                                                              i['biyoloji'],
                                                          biyoname:
                                                              i['biyoname'],
                                                          ses: null)));
                                        }
                                        ;
                                      },

Upvotes: 1

Related Questions