简体   繁体   English

Firebase 数据库返回 null

[英]Firebase database return null

When I read a Firebase database, the city and town variables show me to as null.当我阅读 Firebase 数据库时, citytown变量显示为 null。 Why?为什么?

Denemesiralamaisyerleri deneme = new Denemesiralamaisyerleri();
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String id = firebaseUser.getUid();
DatabaseReference userdata = FirebaseDatabase.getInstance().getReference().child("users").child(id);

ValueEventListener rdn = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) { // Read user info from the database
        deneme = dataSnapshot.getValue(Denemesiralamaisyerleri.class);

    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Toast.makeText(UserInterfaceBerber.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
    }
};

userdata.addValueEventListener(rdn);
city = deneme.getCity();
town = deneme.getTown();
ilin.setText(city);
ilcenin.setText(town);
Toast.makeText(UserInterfaceBerber.this, city + " " + town + " ", Toast.LENGTH_LONG).show();

My database structure like this:我的数据库结构是这样的:

users
igORGoET3gT6i8CV4wvtuN3l08z1
city:
"Antalya"
email:
"ahmetbulur@gmail.com"
namesurname:
"Ahmet bulur"
phonenum:
"05346789445"
town:
"Alanya"

Also, for this toast message, the "city" and "town" variable show as null.此外,对于此toast消息,“city”和“town”变量显示为 null。 I expect that it shows me "Antalya" + "Alanya" for the toast message:我希望它会向我显示“Antalya”+“Alanya”的吐司信息:

Toast.makeText(UserInterfaceBerber.this, city + " " + town + " ", Toast.LENGTH_LONG).show();

Because the data is loaded from Firebase asynchronous, you should use that data only when the operation completes, and this only inside the onDataChange() method, like in the following lines of code:由于数据是从 Firebase 异步加载的,因此您应该仅在操作完成时使用该数据,并且仅在onDataChange()方法中使用该数据,如以下代码行所示:

Denemesiralamaisyerleri deneme=new Denemesiralamaisyerleri();
FirebaseUser firebaseUser=firebaseAuth.getCurrentUser();
String id=firebaseUser.getUid();
DatabaseReference userdata=FirebaseDatabase.getInstance().getReference().child("users").child(id);
ValueEventListener rdn=new ValueEventListener() {
    @Override
    public void onDataChange( DataSnapshot dataSnapshot) {     //read user info from database
        deneme=dataSnapshot.getValue(Denemesiralamaisyerleri.class);
        city=deneme.getCity();
        town=deneme.getTown();
        ilin.setText(city);
        ilcenin.setText(town);
        Toast.makeText(UserInterfaceBerber.this,city+" "+town+" ",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Toast.makeText(UserInterfaceBerber.this,databaseError.getMessage(),Toast.LENGTH_LONG).show();
    }
};
userdata.addValueEventListener(rdn);

For more info, I recommend you see the last part of my answer from this post in which I have explained how it can be done using a custom callback.有关更多信息,我建议您从这篇文章中查看我的答案的最后一部分,其中我解释了如何使用自定义回调来完成。 You can also take a look at this video for a better understanding.您也可以观看此视频以更好地理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM