【问题标题】:How to Compare to Lists in flutter如何在 flutter 中与列表进行比较
【发布时间】:2023-01-10 23:43:59
【问题描述】:

我想要比较/过滤我的移动联系人列表,列表形式为 firebase 用户集合。 两个列表项都包含数字字段,我想显示包含联系人列表中相同号码的 firebase 用户。

 Widget build(BuildContext context) {
    final contactListProvider = Provider.of<ContactProvider>(context);
    return Scaffold(
      appBar: Custom_appbar(
        title: 'Trusted Contacts',
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.only(top: 8.0),
          child: contactListProvider.loadingcontact
              ? Center(
                  child: CircularProgressIndicator(),
                )
              : StreamBuilder<List<UserModel>?>(
                  stream: MyFirebaseDB().userdata(),
                  initialData: [],
                  builder: (context, snapshot) {
                    if (!snapshot.hasData) {
                      return Center(
                        child: CircularProgressIndicator(),
                      );
                    }
                    final usersFirebase = snapshot.data;

                    if (snapshot.hasData) {
                      return ListView.builder(
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: usersFirebase!.length,
                        itemBuilder: (BuildContext context, int index) {
                          var fireuser = usersFirebase[index];
                          var contact = contactListProvider.usercontactlist[index];

                          return Text(fireuser.userNumber.toString());

                     
                        },
                      );
                    } else {
                      return Text('something wrong');
                    }
                  },
                ), 

  ),
      ),
    );
  }

【问题讨论】:

    标签: flutter firebase dart


    【解决方案1】:

    您可以使用 collection package 中的 groupListsBy() 按编号映射其中一个列表。

    然后使用Iterable.where() 扫描第二个列表以查找地图中存在的数字。

    例如:

    import 'package:collection/collection.dart';
    
    class Contact {
      final String name;
      final String number;
      Contact(this.name, this.number);
      @override String toString() => '$name ($number)';
    }
    
    List<Contact> findCommonNumbers(List<Contact> list1, List<Contact> list2) {
      var map = list1.groupListsBy((e) => e.number);
      return list2.where((e) => map.containsKey(e.number)).toList();
    }
    
    main() {
      var contacts = [
        Contact('Adam', '12345'),
        Contact('Bob', '77889'),
        Contact('Charlie', '33221'),
      ];
      var fromDatabase = [
        Contact('April', '45678'),
        Contact('Bobby', '77889'),
        Contact('Chris', '76543'),
      ];
      print(findCommonNumbers(contacts, fromDatabase));
    }
    

    哪些输出:

    [Bobby (77889)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      相关资源
      最近更新 更多