How to get a document contents from Firestore by custom object? (Android - Kotlin)
I'm trying to get a document contents from Firestore. The following image link shows the database structure Firestore database structure
What I want: I want to get a document contents by a custom object and add the contents to a list.
The problem: I'm getting this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aalmesbah.turoodpilot, PID: 12160
java.lang.RuntimeException: Could not deserialize object. Class com.google.firebase.auth.UserInfo does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped
I tried to get the document content by get() and getString() methods and it worked fine the problem is only with the toObject()?
I've searched and tried some suggested solutions from other questions here like add default values for the data class, but it didn't work, unfortunately.
data class code:
data class UserInfo (val name: String? = "",
val email: String? = "",
val phoneNum: String? = "",
val address: String? = "") {
constructor(): this("","","", "" )
}
Profile Fragment code: (where the document contents suppose to be shown)
class ProfileFragment : Fragment() {
private lateinit var auth: FirebaseAuth
private lateinit var db: FirebaseFirestore
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_profile, container, false)
auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()
return view
}
override fun onStart() {
super.onStart()
val userID = auth.currentUser?.uid
val docRef = db.collection("users").document(userID!!)
docRef.addSnapshotListener(EventListener<DocumentSnapshot> { documentSnapshot, e ->
if (e != null) {
Log.w(TAG, "Listen failed.", e)
return@EventListener
}
if (documentSnapshot != null && documentSnapshot.exists()) {
docRef.get().addOnSuccessListener { documentSnapshot ->
val userInfo = documentSnapshot.toObject(UserInfo::class.java)
emailTV.text = userInfo?.email
}
} else {
Log.d(TAG, "Current data: null")
}
})
}
}
sendUserData() method code at Registration activity
private fun sendUserData() {
val name = userN.text.toString()
val email = userEm.text.toString()
val phone = userPhone.text.toString()
val addressName = addressName.text.toString()
val area = area.text.toString()
val block = block.text.toString()
val street = strees.text.toString()
val avenue = avenue.text.toString()
val house = house.text.toString()
val floor = floor.text.toString()
val apartment = apartment.text.toString()
val additionalInfo = additional.text.toString()
val address = "Addres Name: $addressName n Area: $area n B: $block ST: $street Av: $avenue H: $housen " +
"Floor: $floor Apartment: $apartment n Notes: $additionalInfo"
val userID = auth.currentUser?.uid
val userData = UserInfo(name, email, phone, address)
db.collection("users").document(userID!!).set(userData).addOnSuccessListener {
Toast.makeText(this, "Successfully Registered", Toast.LENGTH_SHORT).show()
}.addOnFailureListener{
Toast.makeText(this, "Data Upload error!", Toast.LENGTH_SHORT).show()
}
}

add a comment |
I'm trying to get a document contents from Firestore. The following image link shows the database structure Firestore database structure
What I want: I want to get a document contents by a custom object and add the contents to a list.
The problem: I'm getting this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aalmesbah.turoodpilot, PID: 12160
java.lang.RuntimeException: Could not deserialize object. Class com.google.firebase.auth.UserInfo does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped
I tried to get the document content by get() and getString() methods and it worked fine the problem is only with the toObject()?
I've searched and tried some suggested solutions from other questions here like add default values for the data class, but it didn't work, unfortunately.
data class code:
data class UserInfo (val name: String? = "",
val email: String? = "",
val phoneNum: String? = "",
val address: String? = "") {
constructor(): this("","","", "" )
}
Profile Fragment code: (where the document contents suppose to be shown)
class ProfileFragment : Fragment() {
private lateinit var auth: FirebaseAuth
private lateinit var db: FirebaseFirestore
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_profile, container, false)
auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()
return view
}
override fun onStart() {
super.onStart()
val userID = auth.currentUser?.uid
val docRef = db.collection("users").document(userID!!)
docRef.addSnapshotListener(EventListener<DocumentSnapshot> { documentSnapshot, e ->
if (e != null) {
Log.w(TAG, "Listen failed.", e)
return@EventListener
}
if (documentSnapshot != null && documentSnapshot.exists()) {
docRef.get().addOnSuccessListener { documentSnapshot ->
val userInfo = documentSnapshot.toObject(UserInfo::class.java)
emailTV.text = userInfo?.email
}
} else {
Log.d(TAG, "Current data: null")
}
})
}
}
sendUserData() method code at Registration activity
private fun sendUserData() {
val name = userN.text.toString()
val email = userEm.text.toString()
val phone = userPhone.text.toString()
val addressName = addressName.text.toString()
val area = area.text.toString()
val block = block.text.toString()
val street = strees.text.toString()
val avenue = avenue.text.toString()
val house = house.text.toString()
val floor = floor.text.toString()
val apartment = apartment.text.toString()
val additionalInfo = additional.text.toString()
val address = "Addres Name: $addressName n Area: $area n B: $block ST: $street Av: $avenue H: $housen " +
"Floor: $floor Apartment: $apartment n Notes: $additionalInfo"
val userID = auth.currentUser?.uid
val userData = UserInfo(name, email, phone, address)
db.collection("users").document(userID!!).set(userData).addOnSuccessListener {
Toast.makeText(this, "Successfully Registered", Toast.LENGTH_SHORT).show()
}.addOnFailureListener{
Toast.makeText(this, "Data Upload error!", Toast.LENGTH_SHORT).show()
}
}

add a comment |
I'm trying to get a document contents from Firestore. The following image link shows the database structure Firestore database structure
What I want: I want to get a document contents by a custom object and add the contents to a list.
The problem: I'm getting this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aalmesbah.turoodpilot, PID: 12160
java.lang.RuntimeException: Could not deserialize object. Class com.google.firebase.auth.UserInfo does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped
I tried to get the document content by get() and getString() methods and it worked fine the problem is only with the toObject()?
I've searched and tried some suggested solutions from other questions here like add default values for the data class, but it didn't work, unfortunately.
data class code:
data class UserInfo (val name: String? = "",
val email: String? = "",
val phoneNum: String? = "",
val address: String? = "") {
constructor(): this("","","", "" )
}
Profile Fragment code: (where the document contents suppose to be shown)
class ProfileFragment : Fragment() {
private lateinit var auth: FirebaseAuth
private lateinit var db: FirebaseFirestore
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_profile, container, false)
auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()
return view
}
override fun onStart() {
super.onStart()
val userID = auth.currentUser?.uid
val docRef = db.collection("users").document(userID!!)
docRef.addSnapshotListener(EventListener<DocumentSnapshot> { documentSnapshot, e ->
if (e != null) {
Log.w(TAG, "Listen failed.", e)
return@EventListener
}
if (documentSnapshot != null && documentSnapshot.exists()) {
docRef.get().addOnSuccessListener { documentSnapshot ->
val userInfo = documentSnapshot.toObject(UserInfo::class.java)
emailTV.text = userInfo?.email
}
} else {
Log.d(TAG, "Current data: null")
}
})
}
}
sendUserData() method code at Registration activity
private fun sendUserData() {
val name = userN.text.toString()
val email = userEm.text.toString()
val phone = userPhone.text.toString()
val addressName = addressName.text.toString()
val area = area.text.toString()
val block = block.text.toString()
val street = strees.text.toString()
val avenue = avenue.text.toString()
val house = house.text.toString()
val floor = floor.text.toString()
val apartment = apartment.text.toString()
val additionalInfo = additional.text.toString()
val address = "Addres Name: $addressName n Area: $area n B: $block ST: $street Av: $avenue H: $housen " +
"Floor: $floor Apartment: $apartment n Notes: $additionalInfo"
val userID = auth.currentUser?.uid
val userData = UserInfo(name, email, phone, address)
db.collection("users").document(userID!!).set(userData).addOnSuccessListener {
Toast.makeText(this, "Successfully Registered", Toast.LENGTH_SHORT).show()
}.addOnFailureListener{
Toast.makeText(this, "Data Upload error!", Toast.LENGTH_SHORT).show()
}
}

I'm trying to get a document contents from Firestore. The following image link shows the database structure Firestore database structure
What I want: I want to get a document contents by a custom object and add the contents to a list.
The problem: I'm getting this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aalmesbah.turoodpilot, PID: 12160
java.lang.RuntimeException: Could not deserialize object. Class com.google.firebase.auth.UserInfo does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped
I tried to get the document content by get() and getString() methods and it worked fine the problem is only with the toObject()?
I've searched and tried some suggested solutions from other questions here like add default values for the data class, but it didn't work, unfortunately.
data class code:
data class UserInfo (val name: String? = "",
val email: String? = "",
val phoneNum: String? = "",
val address: String? = "") {
constructor(): this("","","", "" )
}
Profile Fragment code: (where the document contents suppose to be shown)
class ProfileFragment : Fragment() {
private lateinit var auth: FirebaseAuth
private lateinit var db: FirebaseFirestore
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_profile, container, false)
auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()
return view
}
override fun onStart() {
super.onStart()
val userID = auth.currentUser?.uid
val docRef = db.collection("users").document(userID!!)
docRef.addSnapshotListener(EventListener<DocumentSnapshot> { documentSnapshot, e ->
if (e != null) {
Log.w(TAG, "Listen failed.", e)
return@EventListener
}
if (documentSnapshot != null && documentSnapshot.exists()) {
docRef.get().addOnSuccessListener { documentSnapshot ->
val userInfo = documentSnapshot.toObject(UserInfo::class.java)
emailTV.text = userInfo?.email
}
} else {
Log.d(TAG, "Current data: null")
}
})
}
}
sendUserData() method code at Registration activity
private fun sendUserData() {
val name = userN.text.toString()
val email = userEm.text.toString()
val phone = userPhone.text.toString()
val addressName = addressName.text.toString()
val area = area.text.toString()
val block = block.text.toString()
val street = strees.text.toString()
val avenue = avenue.text.toString()
val house = house.text.toString()
val floor = floor.text.toString()
val apartment = apartment.text.toString()
val additionalInfo = additional.text.toString()
val address = "Addres Name: $addressName n Area: $area n B: $block ST: $street Av: $avenue H: $housen " +
"Floor: $floor Apartment: $apartment n Notes: $additionalInfo"
val userID = auth.currentUser?.uid
val userData = UserInfo(name, email, phone, address)
db.collection("users").document(userID!!).set(userData).addOnSuccessListener {
Toast.makeText(this, "Successfully Registered", Toast.LENGTH_SHORT).show()
}.addOnFailureListener{
Toast.makeText(this, "Data Upload error!", Toast.LENGTH_SHORT).show()
}
}


edited Jan 1 at 12:03
A. Almesbah
asked Jan 1 at 8:49


A. AlmesbahA. Almesbah
205
205
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If you want to use a Kotlin data class with documentSnapshot.toObject
, you're going to have to make each field a nullable var
instead of val
. The Firestore SDK doesn't know how to map document fields into data class constructor argument.
If you want a proper immutable data class with val
fields, you're going to have to manually read each field out of the document, and call the data class constructor yourself.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994146%2fhow-to-get-a-document-contents-from-firestore-by-custom-object-android-kotli%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to use a Kotlin data class with documentSnapshot.toObject
, you're going to have to make each field a nullable var
instead of val
. The Firestore SDK doesn't know how to map document fields into data class constructor argument.
If you want a proper immutable data class with val
fields, you're going to have to manually read each field out of the document, and call the data class constructor yourself.
add a comment |
If you want to use a Kotlin data class with documentSnapshot.toObject
, you're going to have to make each field a nullable var
instead of val
. The Firestore SDK doesn't know how to map document fields into data class constructor argument.
If you want a proper immutable data class with val
fields, you're going to have to manually read each field out of the document, and call the data class constructor yourself.
add a comment |
If you want to use a Kotlin data class with documentSnapshot.toObject
, you're going to have to make each field a nullable var
instead of val
. The Firestore SDK doesn't know how to map document fields into data class constructor argument.
If you want a proper immutable data class with val
fields, you're going to have to manually read each field out of the document, and call the data class constructor yourself.
If you want to use a Kotlin data class with documentSnapshot.toObject
, you're going to have to make each field a nullable var
instead of val
. The Firestore SDK doesn't know how to map document fields into data class constructor argument.
If you want a proper immutable data class with val
fields, you're going to have to manually read each field out of the document, and call the data class constructor yourself.
answered Jan 1 at 18:39


Doug StevensonDoug Stevenson
79.2k994112
79.2k994112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994146%2fhow-to-get-a-document-contents-from-firestore-by-custom-object-android-kotli%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown