我在Kotlin的开头,所以我想转换这部分代码:
Query query = reference.child("users");
query.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
Users users = null;
for (DataSnapshot d: dataSnapshot.getChildren()) {
users = d.getValue(Users.class);
if (users.getEmail().equals(mail)) {
photoUri = users.getPhotos();
nameSurname = users.getNome() + " " + users.getCognome();
break;
}
}
if (photoUri != null) {
Glide.with(MainActivity.this)
.load(photoUri)
.transform(new CircleTransform(MainActivity.this))
.into(profile);
}
else {
Glide.with(MainActivity.this)
.load(graphic)
.transform(new CircleTransform(MainActivity.this))
.into(profile);
}
}
public void onCancelled(DatabaseError databaseError) {
}
});
对科特林来说。所以我已经制作了这段代码:
class DataReferenceService @Inject constructor(private var auth: FirebaseDatabase)
: MainContract.DatabaseRepository,ValueEventListener {
var mail: String? = null
override fun getImageProfile(mail: String?) {
this.mail = mail
val query = auth.reference.child("users")
query.addValueEventListener(this)
}
override fun onCancelled(p0: DatabaseError?) {
}
override fun onDataChange(data: DataSnapshot?) {
var user: User? = null
data?.children?.forEach {
child -> getPhotos(child)
}
}
fun getPhotos(child: DataSnapshot) {
val user = child.getValue(User.class) // name expected, why ???
}
}
问题在评论中表明,为什么它不转换它?
还有其他办法吗?我想使用takeIf{}
,但我不了解如何使用它。
由于