Home > Software design >  How can I write data to arraylist in Firebase?
How can I write data to arraylist in Firebase?

Time:06-13

I used this method, I got two errors.

First Error:

A value of type 'Future<String>' can't be assigned to a variable of type 'List<dynamic>'. Try changing the type of the variable, or casting the right-hand type to 'List<dynamic>'.

when I used this method:

List newPhotoUrl = StroageMethods().uploadImageToStroage('Posts', file, true);

second I wrote this ways.

try {
      List newPhotoUrl =
          StroageMethods().uploadImageToStroage('Posts', file, true);
      String postid = _firestore.collection('Posts').doc().id;
      Post post = Post(
        postImages: FieldValue.arrayUnion([{'0': newPhotoUrl}]),
        postid: postid,
        postimage: photoUrl,
        profile_image: profImage,
        publisher: publisher,
        like: [],
      );
      _firestore.collection('Posts').doc(postid).set(
            post.toJson(),
          );
      res = "Successful";
      print(post);
    } catch (err) {
      res = err.toString();
    }

This line got error

postImages: FieldValue.arrayUnion([{'0': newPhotoUrl}]),

The error code was:

The argument type 'FieldValue' can't be assigned to the parameter type 'String'.

CodePudding user response:

try add it

await _firestore.collection('Posts').doc(postid).update({
        'postImages': FieldValue.arrayUnion([photoUrl]),
      });
  • Related