Type mismatch: inferred type is String? but String was expected

北慕城南 提交于 2020-12-14 13:13:51

问题


Due to this line in my codes :

var myRef = child_myRef.child(FirebaseAuth.getInstance().uid) 

Type mismatch: inferred type is String? but String was expecte


回答1:


The reason because your FirebaseAuth.getInstance().uid is String? (can be Null), but input of .child() need a String (none Null), so please check null before that:

FirebaseAuth.getInstance().uid?.let{
  var myRef = child_myRef.child(it) 
  .....
}

Or if you make sure FirebaseAuth.getInstance().uid never Null, you can using FirebaseAuth.getInstance().uid!!, but It not is a good approach




回答2:


You can replace it with child_myRef.child(FirebaseAuth.getInstance().uid!!) or check for null.




回答3:


var myRef = child_myRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
myRef.child("jina").setValue(Fname.text.toString())
myRef.child("tarehe").setValue(bDate.text.toString())
myRef.child("place").setValue(place.text.toString())


Hope you are trying to put all the details under user id.
So if thats the case kindly change your reference to myRef.
So you can put it under your user id all the above details.




回答4:


Its because the value of FirebaseAuth.getInstance().uid can be null , so first check if its not null , then perform your operation .

if(FirebaseAuth.getInstance().uid!=null){
var myRef = child_myRef.child(FirebaseAuth.getInstance().uid)
myRef.child("jina").setValue(Fname.text.toString())
myRef.child("tarehe").setValue(bDate.text.toString())
myRef.child("place").setValue(place.text.toString())

}

If you check with FirebaseAuth.getInstance().uid!! this will cause a Null Pointer Exception and your app will crash if FirebaseAuth.getInstance().uid value comes as null, so try the above approach (as i wrote sample)




回答5:


This line works : child_myRef.child(FirebaseAuth.getInstance().uid!!) and now it is good.

My codes now are like the second image. Thak you all for your help.



来源:https://stackoverflow.com/questions/52921767/type-mismatch-inferred-type-is-string-but-string-was-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!