Arrays 创建一个空数组并将其发送到Firestore,并在Flutter中使用空值?
我想在设置提要时在Firestore中创建一个空数组。但是数组在Firestore中显示为null。这是我的密码。请帮忙Arrays 创建一个空数组并将其发送到Firestore,并在Flutter中使用空值?,arrays,firebase,flutter,google-cloud-firestore,to-json,Arrays,Firebase,Flutter,Google Cloud Firestore,To Json,我想在设置提要时在Firestore中创建一个空数组。但是数组在Firestore中显示为null。这是我的密码。请帮忙 class FeedModel { final String imgUrl; final String desc; final String authorName; final String profileImg; final String title; final int likeCou
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
};
}
}
类模型{
最终字符串imgUrl;
最终字符串描述;
最终字符串authorName;
最终字符串轮廓图;
最后的字符串标题;
最终的整数类计数;
List strArr=[];//这是我要在firestore中创建的数组
FeedModel({this.imgUrl,this.desc,this.authorName,this.profileImg,this.title,this.likeCount,this.strArr});
映射toMap(){
返回{
“imgUrl”:this.imgUrl,
“desc”:this.desc,
“authorName”:this.authorName,
“profileImg”:this.profileImg,
“like_count”:这个,
“用户id”:this.strArr
};
}
}
以下是发送数据代码:
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);
}
}
Future\u sendData()异步{
试一试{
final StorageReference firebaseStorageRef=FirebaseStorage.instance.ref().child('myimage.jpg');
最终StorageUploadTask任务=firebaseStorageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot=wait task.onComplete;
String downloadUrl=await taskSnapshot.ref.getDownloadURL();
最后一个字符串pname=myController.text;
最终字符串pimgurl=下载URL;
最终字符串pauthorName=“Sachin Tendulkar”;
最终字符串pprofileImg=”https://i.picsum.photos/id/564/200/200.jpg?hmac=uExb18W9rplmCwAJ9SS5NVsLaurpaCTCBuHZdhsW25I";
最终字符串ptitle=“演示数据”;
最终整数plikecont=0;
List pLikeduserId;//该行返回空值,如图所示
印刷品(pimgurl);
最终FeedModel feeds=FeedModel(imgUrl:pimgurl,desc:pname,authorName:pauthorName,profileImg:pprofileImg,title:ptitle,likeCount:plikeCount,strArr:pLikeduserId);
insertData(feeds.toMap());
}捕获(e){
印刷品(e);
}
}
空图像:
想要得到如下图片:
创建提要时,如何像上次图像那样发送数组?没有太多理由将其存储为值为
'
的数组。相反,在从firebase接收值时,最好使用如下方式进行空检查:liked\u userid=/*插入获取firebase用户ID的方法*/?[]
这样,当您使用用户ID时,它将不会为null,并且将是一个空列表
如果您确实想要一个内部值为'
的列表,请更改toMap函数的内部:
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
};
}
Map toMap(){
返回{
“imgUrl”:this.imgUrl,
“desc”:this.desc,
“authorName”:this.authorName,
“profileImg”:this.profileImg,
“like_count”:这个,
“喜欢的用户id”:this.strArr=[]?['']:this.strArr//line已更改
};
}
这将使您有一个内有['']的数组,但我仍然推荐我展示的第一种方法。如果您希望字段中有一个数组,甚至是一个空数组,则必须为其指定一个实际的列表值。现在,通过根本不分配任何实际的列表值,您实际上是在为喜欢的用户id分配一个空值 所以,只要给它一个值:
List<String> pLikeduserId = [];
List pLikeduserId=[];
这将写入一个空列表字段。这样做可以创建一个带有空字符串的数组
Map<String, dynamic> toMap() {
return {
'liked_user_id': List.generate(1, (r) => "")
};
}
Map toMap(){
返回{
'liked_user_id':List.generate(1,(r)=>“”)
};
}
等待fireStore.collection(“collection\u NAME”).document(“DoDocument\u NAME”).setData({
“关键值”:[],
});
这将在云FireStore中创建一个空数组