Flutter过滤Json数据

huangapple go评论61阅读模式
英文:

Flutter filtering Json data

问题

我有一个JSON数据,我想要筛选并获取具有特定值的数据。首先,我获取了JSON数据并将其存储在一个列表中。

List _amh = [];
Future<void> readAmh() async {
  final String response = await rootBundle.loadString('assets/json/amh.json');
  final data = await json.decode(response);

  setState(() {
    _amh = data["items"];
  });
}

所以JSON数据如下所示:

{
   "items": [
      {
         "chapter": 1,
         "verse": 1,
         "text": "Lorem Ipsum ....."
      },
      {
         "chapter": 1,
         "verse": 2,
         "text": "Lorem Ipsum ....."
      },
      {
         "chapter": 1,
         "verse": 3,
         "text": "Lorem Ipsum ....."
      },
      ....
      {
         "chapter": 2,
         "verse": 1,
         "text": "Lorem Ipsum ....."
      },
      {
         "chapter": 2,
         "verse": 2,
         "text": "Lorem Ipsum ....."
      },
      ...
   ]
}

所以我想要的是,当用户选择第一章时,我想要显示与该章相关的数据。我该如何做到这一点?

英文:

I have a json data which i wan t to filter and get data that has specific value. first i got the json data and store it in a list.

List _amh = [];
  Future&lt;void&gt; readAmh() async {
    final String response = await rootBundle.loadString(&#39;assets/json/amh.json&#39;);
    final data = await json.decode(response);

    setState(() {
      _amh = data[&quot;items&quot;];
    });
  }

so the json data looks like

{
   &quot;items&quot;: [
      {
         &quot;chapter&quot;: 1,
         &quot;verse&quot;: 1.
         &quot;text&quot;: &quot;Lorem Ipsum .....&quot;
      },
      {
         &quot;chapter&quot;: 1,
         &quot;verse&quot;: 2.
         &quot;text&quot;: &quot;Lorem Ipsum .....&quot;
      },
      {
         &quot;chapter&quot;: 1,
         &quot;verse&quot;: 3.
         &quot;text&quot;: &quot;Lorem Ipsum .....&quot;
      },
      ....
      {
         &quot;chapter&quot;: 2,
         &quot;verse&quot;: 1.
         &quot;text&quot;: &quot;Lorem Ipsum .....&quot;
      },
      {
         &quot;chapter&quot;: 2,
         &quot;verse&quot;: 2.
         &quot;text&quot;: &quot;Lorem Ipsum .....&quot;
      },
      ...
      
   ]
}

so what i wanted is that when the user selects chapter one, i wanted to display the data related to that chapter. how can i do that?

答案1

得分: 1

你可以可能使用以下代码:

List getChapterData(int chapter) {
return _amh.where((item) => item["chapter"] == chapter).toList();
}

**编辑:**

假设 `_selectedChapter = 1`,你可以使用 `ListView` 来显示 `text`:

body: ListView.builder(
itemCount: getChapterData(_selectedChapter).length,
itemBuilder: (BuildContext context, int index) {
final verse = getChapterData(_selectedChapter)[index];
return ListTile(
title: Text('Verse ${verse["verse"]}'),
subtitle: Text(verse["text"]),
);
},
),
);


<details>
<summary>英文:</summary>

You could possibly use

List<dynamic> getChapterData(int chapter) {
return _amh.where((item) => item["chapter"] == chapter).toList();
}

**Edit:** 

Assuming the `_selectedChapter = 1` , you can use `ListView` to display the `text`
  body: ListView.builder(
    itemCount: getChapterData(_selectedChapter).length,
    itemBuilder: (BuildContext context, int index) {
      final verse = getChapterData(_selectedChapter)[index];
      return ListTile(
        title: Text(&#39;Verse ${verse[&quot;verse&quot;]}&#39;),
        subtitle: Text(verse[&quot;text&quot;]),
      );
    },
  ),
);

</details>



huangapple
  • 本文由 发表于 2023年3月12日 06:51:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定