1

I'm trying to create an application that can read the values from a Firestore array and put them into an ArrayList or something, this is my code:

    ArrayList<Integer> driverPermissions = new ArrayList<>();

    Firestore.collection("Admins").document("1234567890").get().addOnCompleteListener(task -> {
        DocumentSnapshot document = task.getResult();
        driverPermissions = document.get("Test");
    });

Unfortunately, it doesn't work. It marks me as an error in this line:

driverPermissions = document.get("Test");

And the error is:

Variable used in lambda expression should be final or effectively final

How can I add the values present inside the array on Firestore into an ArrayList or something like that and then use that throughout the code? And not only within the method?

Andrea
  • 21
  • 6

3 Answers3

0

Try This

 driverPermissions = document.getString("Test");
0

Your trying to get data type String but You declared Array tn Integer first mistake.

Then try this

Firestore.collection("Admins").document("1234567890").get().
addOnCompleteListener(task -> {
    DocumentSnapshot document = task.getResult();
   ArrayList<String> driverPermissions = (ArrayList<String>) 
   document.get("Test");
});

Here you can store only store string type data.

Or you can try like this

ArrayList<Object> driverPermissions = New ArrayList<>();

Firestore.collection("Admins").document("1234567890").get().
addOnCompleteListener(task -> {
    DocumentSnapshot document = task.getResult();
   driverPermissions.add(new String(document.get("Test")); // When Storing STring
driverPermissions.add(new Integer(document.get("Test")); // When Storing Integer

});

Here you can store any kind of data can be integer can be string But You have to declared what kind of data your storing.

On Demand Way

ArrayList<String> driverPermissions = New ArrayList<String>();

Firestore.collection("Admins").document("1234567890").get().
addOnCompleteListener(task -> {
   DocumentSnapshot document = task.getResult();
   driverPermissions.add(document.get("Test")); Or
   driverPermissions.add(""+document.get("Test"));
});

Another way

 ArrayList<String> driverPermissions;
Firestore.collection("Admins").document("1234567890").get().
addOnCompleteListener(task -> {
    DocumentSnapshot document = task.getResult();
   driverPermissions = (ArrayList<String>) 
   document.get("Test");
});
Kayes Fahim
  • 672
  • 5
  • 16
  • Hi Kayes, the first code only works if I want to use the list inside the method, in fact it doesn't recognize it outside. The second code is what I am trying to do since the list, being declared outside the method, can be safely used throughout the code. For how you wrote the second code it gives an error: Cannot resolve constructor 'String (java.lang.Object)' What can I do? – Andrea Aug 06 '22 at 12:30
  • second code you can keep any kind of data just you have cast type of data which you gonna trying to store. – Kayes Fahim Aug 06 '22 at 12:35
  • The array in the database consists of strings only. In your second code I have kept only the part that works with them and not with integers. But it doesn't work, the error is what I wrote before. – Andrea Aug 06 '22 at 12:39
  • Then Replace Object As String on second code , and inside remove type cast new String Part and Paranthesis as well , Hope it will work. – Kayes Fahim Aug 06 '22 at 12:42
  • I did as you said, but nothing. It gives me an error: Cast parameter to java.lang.string and suggests me to change the line of code like this: driverPermissions.add ((String) document.get ("Test")). But once I run the code it crashes and generates this error: java.util.ArrayList cannot be cast to java.lang.String – Andrea Aug 06 '22 at 12:51
  • ArrayList driverPermissions = New ArrayList(); driverPermissions.add(document.get ("Test")); – Kayes Fahim Aug 06 '22 at 12:55
  • I did this, it doesn't work – Andrea Aug 06 '22 at 12:59
  • I added new way and Change The code check please – Kayes Fahim Aug 06 '22 at 13:03
  • In thise line: driverPermissions = (ArrayList) gives me an error: Variable used in lambda expression should be final or effectively final – Andrea Aug 06 '22 at 13:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247080/discussion-between-andrea-and-kayes-fahim). – Andrea Aug 06 '22 at 13:13
0

A variable that is used in a lambda expression should always be final or effectively final, and you cannot change that. To solve this, you either make the variable a member of the class or define it and initialize it inside the onComplete().

How can I add the values present inside the array on Firestore into an ArrayList or something like that and then use that throughout the code? And not only within the method?

There is no way you can do that. Firebase API is asynchronous, meaning that any code that needs data from Cloud Firestore, needs to be inside the onComplete() method, or be called from there. You cannot simply use it outside the callback, even if the field is a member of the class. So to solve this you can use a custom callback as seen in that answer. The reason is that you need to wait for the data. If you understand Kotlin, you might also be interested in reading the following resource:

In Java, you should consider using LiveData. I think this repo might also help.

P.S. If you need at some point in time to use an array of objects, here is another helpful resoruce:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193