【问题标题】:Flutter : How to skip one element in map Iteration?Flutter:如何在地图迭代中跳过一个元素?
【发布时间】:2020-12-05 13:37:11
【问题描述】:

我从 Firebase 获取数据,我不想获取 create_uid 与 uid 相同的元素。所以问题是如何在地图上运行迭代时跳过一些元素。

  // 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);
  }

请帮助我。谢谢你。我期待您的来信。

【问题讨论】:

    标签: dictionary flutter dart iterator snapshot


    【解决方案1】:

    让我们用一个简单的例子:

    给定一个项目列表(在本例中为 int)和一个可能返回 null 的“处理”函数,我们希望返回一个已处理项目的数组没有 null 值。

    如果我们想使用 map 函数,我们不能跳过项目,所以我们必须首先处理项目,然后用 where 过滤掉它们,然后强制转换为 not null(为了 null 安全)。

    另一个选择是使用扩展函数,因为如果返回一个空数组,它将跳过该项目

    示例代码:

    ///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");
    }
    

    您还可以展开迭代器以便于访问:

    ///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");
    }
    

    【讨论】:

      【解决方案2】:
       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();
        }
      

      第二个解决方案

        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;
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-11
        • 1970-01-01
        相关资源
        最近更新 更多