Create an empty array and send it to Firestore with an empty value in Flutter?
1,305
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.
Author by
TB13
Updated on December 24, 2022Comments
-
TB13 over 1 year
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); } }
How can i send array like last image when creating a feed?