Home > Net >  Read arrays from Firestore and put them inside an ArrayList - Java in Android Studio
Read arrays from Firestore and put them inside an ArrayList - Java in Android Studio

Time:08-06

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?

CodePudding user response:

Try This

 driverPermissions = document.getString("Test");

CodePudding user response:

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.

  • Related