Create an empty array and send it to Firestore with an empty value in Flutter?

3k views Asked by At

I want to create an empty array in Firestore while posing a feed. But the array is showing null in Firestore. Here is my code. Please help.

   class FeedModel {
    
      final String imgUrl;
      final String desc;
      final String authorName;
      final String profileImg;
      final String title;
      final int likeCount;
      List<String> strArr = [];   // this the array i want to create it in firestore
    
      FeedModel({this.imgUrl, this.desc,this.authorName,this.profileImg,this.title,this.likeCount, this.strArr});
    
      Map<String, dynamic> toMap(){
        return {
         "imgUrl" : this.imgUrl,
          "desc" : this.desc,
          "authorName" : this.authorName,
          "profileImg" : this.profileImg,
          "like_count" : this.likeCount,
          "liked_user_id" : this.strArr
        };
      }
   
    }

Here is the send data code:

Future<void> _sendData() async {

    try {
      final StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child('myimage.jpg');
      final StorageUploadTask task = firebaseStorageRef.putFile(_image);
      StorageTaskSnapshot taskSnapshot = await task.onComplete;
      String downloadUrl = await taskSnapshot.ref.getDownloadURL();
      final String pname = myController.text;
      final  String pimgurl = downloadUrl;
      final String pauthorName = "Sachin Tendulkar";
      final String pprofileImg = "https://i.picsum.photos/id/564/200/200.jpg?hmac=uExb18W9rplmCwAJ9SS5NVsLaurpaCTCBuHZdhsW25I";
      final String ptitle = "Demo Data";
      final int plikeCount= 0;
      List<String> pLikeduserId;  // This line returning null as show in image
      print(pimgurl);
      final FeedModel feeds = FeedModel(imgUrl: pimgurl ,desc: pname,authorName: pauthorName ,profileImg: pprofileImg,title: ptitle,likeCount: plikeCount, strArr : pLikeduserId );
      insertData(feeds.toMap());

    }  catch (e) {
      print(e);
    }

  }

null image: enter image description here

want to get like below image: enter image description here

How can i send array like last image when creating a feed?

4

There are 4 answers

0
Doug Stevenson On BEST ANSWER

If you want an array in a field, even an empty array, you will have to assign it an actual list value. Right now, by not assigning any actual list value at all, you're effectively assigning a null value to the liked_user_id.

So, just give it a value:

List<String> pLikeduserId = [];

That will write an empty list field.

0
Rushi Donga On

await _fireStore.collection("COLLECTION_NAME").document("DOUCUMENT_NAME").setData({ "KEY_VALUE": [], });

This creates an empty Array in Cloud FireStore.

0
Harry On

There isn't much of a reason to store it as an array with a value of ''. Instead, it would be better to null check when receiving the value from firebase by using something like: liked_userid = /*insert method of getting firebase user ID*/??[]

This way when you use the liked_userid it won't be null and will be an empty list.

If you really want a list with the value of '' inside than change the insides of the toMap function:

Map<String, dynamic> toMap(){
    return {
     "imgUrl" : this.imgUrl,
      "desc" : this.desc,
      "authorName" : this.authorName,
      "profileImg" : this.profileImg,
      "like_count" : this.likeCount,
      "liked_user_id" : this.strArr=[]?['']:this.strArr//line that was changed
    };
  }

This would make it so that you will have an array with [''] inside but I would still recommend the first method I showed.

0
Joseph Utulu On

Do this to create an array with an empty string.

Map<String, dynamic> toMap() {
    return {
        'liked_user_id': List.generate(1, (r) => "")
    };
}