Reputation:
I am getting data from Firebase and I don't want to get the element where create_uid is the same with uid. So the question is how do I skip some elements while run iteration on map.
// list from snapshot
List<ImageProperty> _pictureListFromSnapShot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return ImageProperty(
title: doc.data['title'] ?? null,
filename: doc.data['filename'] ?? null,
token: doc.data['token'] ?? null,
filelocation: doc.data['filelocation'] ?? null,
url: doc.data['url'] ?? null,
created: doc.data['created'] ?? null,
creator_uid: doc.data['creator_uid'] ?? null,
format: doc.data['format'] ?? null,
created_date: doc.data['created_date'].toString() ?? null,
timestamp: doc.data['timestamp'] ?? null,
tag_label: doc.data['tag_label'] ?? null,
user_tag: doc.data['user_tag'] ?? null,
rating: doc.data['rating'] ?? 0,
score: doc.data['score'] ?? 0,
display_count: doc.data['score_display_count'] ?? 0,
judges: doc.data['judges'] ?? null,
isClicked: false,
isShown: false,
);
}).toList();
}
// get test stream
Stream<List<ImageProperty>> get pictureData {
return pictureCollection.snapshots().map(_pictureListFromSnapShot);
}
Please help me. Thank you. I am looking forward to hearing from you.
Upvotes: 1
Views: 7211
Reputation: 784
Let's use a simple example:
given a list of items (int in this case) and a "process" function that may return null we want to return an array of processed items without the null values.
If we want to use the map function we cannot skip the items so we must first process the items, then filter them out with where and then cast to not null (for null safety).
Another option is to use the expand function as it will skip the item if an empty array is returned
Example code:
///Just an example process function
int? process(int val) => val > 0 ? val * 100 : null;
void main() {
/// Our original array
final List<int> orig = [1, 2, -5, 3, 0, -1, -3];
/// Using map and filter
final List<int?> map = orig
.map((e) => process(e)) // <--mapped to results but with null
.where((e) => e != null) //<--remove nulls
.cast<int>() //<-- cast to not nullable as there are none
.toList(); //<-- convert to list
/// Using expand
final filtered = orig.expand((val) {
final int? el = process(val);
return el == null ?
[] : //<-- returning an empty array will skip the item
[el];
}).toList();
print("Origin : $orig");
print("Transformed with map : $map");
print("Transformed with expand : $filtered");
}
You can also expand iterator for easier access:
///Just an example process function
int? process(int val) => val > 0 ? val * 100 : null;
/// Our extension
extension MapNotNull<E> on Iterable<E> {
Iterable<T> mapNotNull<T>(T? Function(E e) transform) => this.expand((el) {
final T? v = transform(el);
return v == null ? [] : [v];
});
}
void main() {
/// Our original array
final List<int> orig = [1, 2, -5, 3, 0, -1, -3];
/// Will automatically skip null values
final List<int> filtered = orig.mapNotNull((e)=>process(e)).toList();
print("Origin : $orig");
print("Filtered : $filtered");
}
Upvotes: 6
Reputation: 909
List<ImageProperty> _pictureListFromSnapShot(QuerySnapshot snapshot) {
List<dynamic> filterlist = snapshot.documents.where((doc){
return doc.data['creator_uid'] !=uid}).toList();
return filterlist.map((doc) {
return ImageProperty(
title: doc.data['title'] ?? null,
filename: doc.data['filename'] ?? null,
token: doc.data['token'] ?? null,
filelocation: doc.data['filelocation'] ?? null,
url: doc.data['url'] ?? null,
created: doc.data['created'] ?? null,
creator_uid: doc.data['creator_uid'] ?? null,
format: doc.data['format'] ?? null,
created_date: doc.data['created_date'].toString() ?? null,
timestamp: doc.data['timestamp'] ?? null,
tag_label: doc.data['tag_label'] ?? null,
user_tag: doc.data['user_tag'] ?? null,
rating: doc.data['rating'] ?? 0,
score: doc.data['score'] ?? 0,
display_count: doc.data['score_display_count'] ?? 0,
judges: doc.data['judges'] ?? null,
isClicked: false,
isShown: false,
);
}).toList();
}
2nd solution
List<ImageProperty> _pictureListFromSnapShot(QuerySnapshot snapshot)
{
List<ImageProperty> filterlist = []
snapshot.documents.forEach((doc) {
if(oc.data['creator_uid'] !=uid){
filterlist.add(ImageProperty(
title: doc.data['title'] ?? null,
filename: doc.data['filename'] ?? null,
token: doc.data['token'] ?? null,
filelocation: doc.data['filelocation'] ?? null,
url: doc.data['url'] ?? null,
created: doc.data['created'] ?? null,
creator_uid: doc.data['creator_uid'] ?? null,
format: doc.data['format'] ?? null,
created_date: doc.data['created_date'].toString() ?? null,
timestamp: doc.data['timestamp'] ?? null,
tag_label: doc.data['tag_label'] ?? null,
user_tag: doc.data['user_tag'] ?? null,
rating: doc.data['rating'] ?? 0,
score: doc.data['score'] ?? 0,
display_count: doc.data['score_display_count'] ?? 0,
judges: doc.data['judges'] ?? null,
isClicked: false,
isShown: false,
));
}
});
return filterlist;
}
Upvotes: 0