【问题标题】:How to filter Future List in Flutter?如何在 Flutter 中过滤未来列表?
【发布时间】:2021-06-13 06:25:56
【问题描述】:

我在过滤列表方面需要帮助。

我在 initState 中填充列表:

  Future _organisations;

  Future<void> readJson() async {
    final String response =
        await rootBundle.loadString('assets/organisations.json');
    final data = await json.decode(response);

    List<Organization> temp = [];

    for (var item in data) {
      Organization place = Organization(
        address: item['Address'],
        contactInfo: item['Contact info'],
        organization: item['Organization'],
        phoneNumber: item['Phone number'],
        shortDescription: item['Short description'],
        subject: item['Subject'],
        tags: item['Tags'],
      );

      temp.add(place);
    }

    temp.sort((a, b) => a.organization.compareTo(b.organization));

    return temp;
  }

  @override
  void initState() {
    super.initState();
    _organisations = readJson();
  }

然后我在列表上方有一个按钮,我想用它来使用 setState 过滤列表,但是它不起作用。如何过滤 Future 类型的列表?

【问题讨论】:

    标签: list flutter dart filter future


    【解决方案1】:

    Future&lt;void&gt; 不返回任何内容。如果你想让它返回一个列表,它应该是Future&lt;List&gt;。当您对列表中的项目进行排序时,您将它们与什么进行比较? item[“组织”]属于什么类型?它必须是可比较的类型。

    【讨论】:

      【解决方案2】:

      首先,您的方法readJson() 的返回值类型为Future&lt;void&gt;,这意味着您将永远无法返回您的List&lt;Organization&gt;

      此外,您正在使用异步操作,这意味着您将需要使用awaitthen 来在某些时候获得您的价值。

      以下是如何修复代码的示例:

      List<Organization> _organisations;
      
      // Simply change the return type to Future<List<Organization>>
      Future<List<Organization>> readJson() async {
        final String response =
          await rootBundle.loadString('assets/organisations.json');
        final data = await json.decode(response);
      
        List<Organization> temp = [];
      
        for (var item in data) {
          Organization place = Organization(
            address: item['Address'],
            contactInfo: item['Contact info'],
            organization: item['Organization'],
            phoneNumber: item['Phone number'],
            shortDescription: item['Short description'],
            subject: item['Subject'],
            tags: item['Tags'],
          );
          temp.add(place);
        }
      
        /// Then you need to compare objects that are comparable.
        /// For example if you want to sort your items by their address :
        temp.sort((a, b) =>
          a.address.toLowerCase().compareTo(b.address.toLowerCase())
        );
      
        return temp;
      }
      
      @override
      void initState() {
        super.initState();
        
        /// As you cannot use await in initState you can use then
        /// to perform an action once your asynchronous operation is
        /// done.
        ///
        /// In this case I am assigning the value to _organisations
        /// and causing a rebuild of the interface with setState.
        readJson().then((List<Organization> temp) {
          setState(() => _organisations = temp);
        });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-07
        • 1970-01-01
        • 2019-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-22
        • 1970-01-01
        相关资源
        最近更新 更多