Skip to content Skip to sidebar Skip to footer

"no Setter/field For Field Found On Class"

I'm creating an app in Android Studio, which connects to a Cloud Firestore database. In the database I have the following structure: Myclass - name = 'test' - subclass - 0

Solution 1:

Your problem right now its that your class name must be the same than the constructor. Also you need to add a getter to your subclass parameter.

publicclassChart {

   privateString name;
   privateString[] subclass;

   publicChart() {
   //Default empty constructor, required for Firebase.
   }

   publicChart(String name, String[] subclass) {
       this.name = name;
       this.subclass = subclass;
   }

   publicStringgetName() {
      returnthis.name;
   }

   publicString[] getSubclass() {
      return subclass;
   }
}

In other hand, you don't need to add the setters. They are not required. Firebase will set the value into the field. But you should add them if you're going to interact with the class from outside.

There will be some cases where you want to have different names on your parameters, maybe because you want to follow a camelCase nomenclature or something. If that's the case you can use the annotation @PropertyName to provide a different name in your database and keep your model as you want. For example:

publicclassChart {

   @PropertyName("name")
   privateString mName;
   @PropertyName("subclass")
   privateString[] mSubclass;

   publicChart() {
   }

   @PropertyName("name")
   publicStringgetmName() {
      return mName;
   }

   @PropertyName("subclass")
   publicString[] getmSubclass() {
      return mSubclass;
   }
}

Solution 2:

You have two errors in your model class. First one would be the name of the constructor which is different than the name of the class and should be the same. And the second, for the subclass field you have only defined the setter but without a getter.

Your Myclass class should look like this:

publicclassMyClass {
    privateString name;
    privateString[] subclass;

    publicMyClass() {}

    publicMyClass(String name, String[] subclass) {
        this.name = name;
        this.subclass = subclass;
    }

    publicStringgetName() { return name; }

    publicString[] getSubclass() { return subclass; }
}

Setters are not not required. If there is no setter for a JSON property, the Firebase client will set the value directly onto the field, that's why is called idiomatic. If you need them explicitly in your code, just add the following setters to your model class like this:

publicvoidsetName(String name) { this.name = name; }

publicvoidsetSubclass(String[] subclass) { this.subclass = subclass; }

Regarding the use of arrays in the Cloud Firestore database, please see my answer from this post.

Post a Comment for ""no Setter/field For Field Found On Class""