简体   繁体   English

在 onComplete 方法中实现接口时出现问题

[英]Problem implementing interface in an onComplete method

Following the previous thread that I have created here: How do I set a variable inside of an onComplete method?按照我在此处创建的上一个主题: 如何在 onComplete 方法中设置变量?

I am still trying to call the variable that I have set inside on the onComplete method.我仍在尝试调用我在onComplete方法中设置的变量。 I know there is a delay in-between accessing the method.我知道访问该方法之间存在延迟。 And the one solution that was brought up to me was to create an interface.向我提出的一个解决方案是创建一个界面。 Although, I have successfully implemented the interface, I am not sure whether my implementation is correct since I am still getting nulls.虽然,我已经成功实现了接口,但我不确定我的实现是否正确,因为我仍然得到空值。

Here's my Interface:这是我的界面:

public interface MyCallBack {
void onCallFullName(String fullName);
void onCallEmail(String Email);
}

The method to fetch the data from firebase:从firebase取数据的方法:

private void callUserCredential(MyCallBack myCallBack) {
    fAuth = FirebaseAuth.getInstance();
    fStore = FirebaseFirestore.getInstance();
    String userID = fAuth.getCurrentUser().getUid();


    DocumentReference docRef = fStore
            .collection("users")
            .document(userID);

    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if(task.isSuccessful()){
                DocumentSnapshot document = task.getResult();
                if(document.exists()){

                    //Log for successfully fetching data from firebase
                    Log.d(TAG,"User full name: " + document.getString("FullName"));
                    Log.d(TAG, "User email: " + document.getString("Email"));

                    String fullName = document.getString("FullName");
                    String eMail = document.getString("Email");

                    //Log for successfully setting the data into the variables
                    Log.d(TAG, "donorFullName set: " + fullName);
                    Log.d(TAG, "donorEmail set: " + eMail);

                    //Interface callback
                    myCallBack.onCallFullName(fullName);
                    myCallBack.onCallEmail(eMail);

                }
            }
        }
    });
}

and Lastly, here's me calling the Interface:最后,这是我调用接口:

//Create booking information
    BookingInformation bookingInformation = new BookingInformation();

    callUserCredential(new MyCallBack() {
        @Override
        public void onCallFullName(String fullName) {
            donorFullName = fullName;
        }

        @Override
        public void onCallEmail(String Email) {
            donorEmail = Email;
        }
    });

    //Log for successfully calling the interface
    Log.d(TAG, "donorFullName bookingInformation: " + donorFullName);
    Log.d(TAG, "donorEmail bookingInformation: " + donorEmail);
    bookingInformation.setDonorName(donorFullName);
    bookingInformation.setDonorEmail(donorEmail);

The logcat still shows the same thing: logcat 仍然显示相同的内容:

在此处输入图像描述

You must setDonorName() and setDonorEmail() in Callback functions like:您必须在回调函数中设置 DonorName() 和 setDonorEmail(),例如:

//Create booking information
    BookingInformation bookingInformation = new BookingInformation();

    callUserCredential(new MyCallBack() {
        @Override
        public void onCallFullName(String fullName) {
            bookingInformation.setDonorName(fullName);
            //Log for successfully calling the interface
            Log.d(TAG, "donorFullName bookingInformation: " + fullName);
        }

        @Override
        public void onCallEmail(String email) {
            bookingInformation.setDonorEmail(email);
            //Log for successfully calling the interface
            Log.d(TAG, "donorEmail bookingInformation: " + email);
        }
    });

Because when you call the log function, the request is uncompleted yet!因为当你调用日志function时,请求还没有完成!

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

相关问题 如何在 onComplete 方法中对 ArrayList 进行排序,firebase - How to sort an ArrayList in onComplete method, firebase 在使用 Firestore 数据库时使用 onComplete() 方法中的变量 - Using variable from onComplete() method while working with Firestore database flutter Firebase 存储完成 - flutter FirebaseStorage onComplete 执行 ListView.builder() 时这段代码有什么问题吗 - Is there any problem in this code while implementing ListView.builder() 登录后 getUid() 在 onComplete 上返回 null - getUid() returns null on onComplete after Login firebase方法结束前另一个方法运行的问题reactjs - Problem of another method runs before the firebase method finishes reactjs Firebase 存储等待 uploadTask.onComplete 过时 - Firebase Storage await uploadTask.onComplete outdated Toast 和新的意图调用在 firebase onComplete 内部不起作用 - Toast and new intent call not working inside firebase onComplete Azure header 中的 AD 承载令牌,用于验证对 web 的请求 API - 更新方法有问题 - Azure AD bearer Token in header to authenticate request to web API - Problem with update method 无法无条件调用方法“[]”,因为接收者可以为“null”,这是什么问题 - The method '[]' can't be unconditionally invoked because the receiver can be 'null' what is the problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM