Class Java.Util.Map Has Generic Type Parameters, Please Use Generictypeindicator Instead

Class java.util.Map has generic type parameters, please use GenericTypeIndicator instead

The error points out correctly where you are going wrong

 Map<String, String> map = dataSnapshot.getValue(Map.class);

Map class uses parameter to define the types of Key and Object where as you don't give them and simply use Map.class which fails.

Try the below code - since Key are always string and we can have any type of Object for them

    Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();

DatabaseException: Class java.util.List has generic type parameters, please use GenericTypeIndicator instead

You cannot convert any object to generic List object. You need to define which type of objects the list accepts.

Firebase provides a GenericTypeIndicator to resolve this.

Usage:

GenericTypeIndicator<List<String>> typeIndicator = new GenericTypeIndicator<List<String>>() {};

And then pass typeIndicator as the type argument in your getValue()
E g.

list = ds.getValue(typeIndicator)

Should solve your problem.

Thanks

Android, Kotlin: Creating a GenericTypeIndicator from reflection

I got the detection of the Map variable working by doing an isAssignableFrom, and creating an object for GenericTypeIndicator.

So the only thing that needed to be changed is the when part in the code to this:

when {
field.type == Translation::class.java ->
field.set(obj, Translation(it.getValue(String::class.java)!!))
Map::class.java.isAssignableFrom(field.type) ->
field.set(obj, it.getValue(object: GenericTypeIndicator<HashMap<String, Any>>(){}))
else -> field.set(obj, it.getValue(field.type))
}

Firebase only accepts String as keys, so we can just make that one fixed, It isn't fond of using * notation for the value though, but we can just make it Any and it seems to work fine.

I hope this helps other people who might ever come across this problem x)



Related Topics



Leave a reply



Submit