I'm trying to create an Athlet Object from a DocumentSnapshot. In this DocumentSnapshot I have stored a map, that holds the values(i.E. "upperbody": 0), to this athletes favourite Categorys. But for the AthleteObject I only need the key, of the category with the highest Value. This should work, but I get the following error. And I dont' know how to rewrite this for it to not complain.
The instance member 'getFavCategory' can't be accessed in an initializer. (Documentation) Try replacing the reference to the instance member with a different expression
Athlete.fromJSON(DocumentSnapshot parsedJSON):
_name = parsedJSON["name"],
_rank = parsedJSON['rank'],
_xp = parsedJSON['xp'],
_foot = parsedJSON['foot'],
_position = parsedJSON['position'],
String getFavCategory(Map<String, dynamic> map){
String favCat;
int maxVal;
map.forEach((key, value) {
if(maxVal == null || value > maxVal){
maxVal = value;
favCat = key;
}
});
return favCat;
}
Your definition of getFavCategory states that it has to be called on an initialized instance of your Athlete class (which is not a case for your constructor).
The fastest you can do is simply turn your getFavCategory function in to static (since you are not using any class variables inside):
static String getFavCategory(Map<String, dynamic> map){
String favCat;
int maxVal;
map.forEach((key, value) {
if(maxVal == null || value > maxVal){
maxVal = value;
favCat = key;
}
});
return favCat;
}