Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Cloud Firestore adding document - Android Studio

pls help me with this tinny issue

I've this android code:

private ArrayList serviceNameList = new ArrayList();

serviceNameList.add("windows");
serviceNameList.add("linux");
serviceNameList.add("mac");

then, i use firestore to save my data:

Map<String, Object> service = new HashMap<>();
            service.put("full_name", full_name.getText().toString());
            service.put("Services_names", Collections.singletonList(serviceNameList));

            firestore.collection("service").document("new_service")
                    .set(service)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(getApplicationContext(), "Success..!",Toast.LENGTH_SHORT).show();
                            }
            })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                            public void onFailure(@NonNull Exception e) {
                                String error = e.getMessage();
                                Toast.makeText(getApplicationContext(), "Error: " + error,Toast.LENGTH_SHORT).show();
                        }
            });

all works fine when i comment or delete this line:

service.put("Services_names",Collections.singletonList(serviceNameList));

But when i enable it again my app fail... I want to know what can i do for solve this...

Thank you..!!!!

btw, this is my logcat

2019-07-08 23:03:04.943 com.comas.app E: FATAL EXCEPTION: main
Process: com.comas.app, PID: 30989
java.lang.IllegalArgumentException: Invalid data. Nested arrays are not supported
    at com.google.firebase.firestore.core.UserData$ParseContext.createError(com.google.firebase:firebase-firestore@@19.0.2:293)
    at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@19.0.2:270)
    at com.google.firebase.firestore.UserDataConverter.parseList(com.google.firebase:firebase-firestore@@19.0.2:307)
    at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@19.0.2:272)
    at com.google.firebase.firestore.UserDataConverter.parseMap(com.google.firebase:firebase-firestore@@19.0.2:294)
    at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@19.0.2:250)
    at com.google.firebase.firestore.UserDataConverter.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@19.0.2:230)
    at com.google.firebase.firestore.UserDataConverter.parseSetData(com.google.firebase:firebase-firestore@@19.0.2:83)
    at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@19.0.2:175)
    at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@19.0.2:154)
    at com.comas.app.PopUpActivity$2.onClick(PopUpActivity.java:173)
    at android.view.View.performClick(View.java:6663)
    at android.view.View.performClickInternal(View.java:6635)
    at android.view.View.access$3100(View.java:794)
    at android.view.View$PerformClick.run(View.java:26199)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7593)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)2019-07-08 23:03:04.955 ? E: FaultDetect: DUMPTOOL_PRINTF return.
like image 687
yuli castaño Avatar asked Nov 06 '22 15:11

yuli castaño


1 Answers

If you want to store an ArrayList data into Firestore, first iterate the entire ArrayList. Then, pass that variable only into HashMap.

private ArrayList serviceNameList = new ArrayList();

serviceNameList.add("windows");
serviceNameList.add("linux");
serviceNameList.add("mac");

Map<String, Object> service = new HashMap<>();
service.put("full_name", full_name.getText().toString());
service.put("Services_names", serviceNameList);

Kotlin

val serviceNameList = ArrayList<Any>()

serviceNameList.add("windows")
serviceNameList.add("linux")
serviceNameList.add("mac")

val service: MutableMap<String, Any> = HashMap()
service["full_name"] = full_name.getText().toString()
service["Services_names"] = serviceNameList
like image 114
Parth Patel Avatar answered Nov 14 '22 21:11

Parth Patel