【问题标题】:How to filter specific data from an API flutter?如何从 API 颤振中过滤特定数据?
【发布时间】:2020-11-18 18:13:47
【问题描述】:

我有一个 API https://sdgfortb.herokuapp.com/onetreeplanted

这就是我获取数据的方式。

Future<List<TreeInfo>> fetchGoals(http.Client client) async {
final response =
  await client.get('https://sdgfortb.herokuapp.com/onetreeplanted');

// Use the compute function to run parseGoalss in a separate isolate.

return compute(parseGoalss, response.body);
 }

// A function that converts a response body into a List<TreeInfo>.
List<TreeInfo> parseGoalss(String responseBody) {
final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();

 return parsed.map<TreeInfo>((json) => TreeInfo.fromJson(json)).toList();
} 

这是我的模特

class TreeInfo {
  String region;
  String country;
  String name;
  String overview;
  String impact;
  String treespecies;
  String imagelink;

  TreeInfo(
      {this.region,
      this.country,
      this.name,
      this.overview,
      this.impact,
      this.treespecies,
      this.imagelink});

  factory TreeInfo.fromJson(Map<String, dynamic> json) {
    return TreeInfo(
      region: json["region"] as String,
      country: json["country"] as String,
      name: json["name"] as String,
      overview: json["overview"] as String,
      treespecies: json["tree_species"] as String,
      impact: json["impact"] as String,
      imagelink: json["image_link"] as String,
    );
  }
}

获取数据 类加载扩展 StatelessWidget { 最终的字符串目的地;

  Loading({this.destination});
  @override
  Widget build(BuildContext context) {    
    return FutureBuilder<List<TreeInfo>>(
      future: fetchGoals(http.Client()),
      builder: (context, snapshot) {
        if (snapshot.hasError) print(snapshot.error);

        return snapshot.hasData
            ? GoalPage(
                goals: snapshot.data,
               // destination: destination,
              )
            : Center(
                child: CircularProgressIndicator(
                backgroundColor: Colors.greenAccent,
              ));
      },
    );
  }
}
class GoalPage extends StatelessWidget {
     
  List<Goals> goals;

  GoalPage({this.goals});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
             Container(
                  height: 470,
                  padding: const EdgeInsets.only(left: 32),
                  child: Swiper(
                    itemCount: goals.length,
                    itemWidth: MediaQuery.of(context).size.width - 2 * 64,
                    layout: SwiperLayout.STACK,
                    pagination: SwiperPagination(
                      builder:
                          DotSwiperPaginationBuilder(activeSize: 8, space: 3),
                    ),
                    itemBuilder: (context, index) {
                      return InkWell(
                        onTap: () {
                          Navigator.push(
                            context,
                            PageRouteBuilder(
                              pageBuilder: (context, a, b) => DetailPage(
                                goalInfo: goals[index],
                              ),
                            ),
                          );
                        },

我想获取包含北美地区的数组。

还有其他三个区域,但我想通过 API 从北美获取数据

【问题讨论】:

    标签: json api flutter filter


    【解决方案1】:

    你可以使用List中的where函数

    final filteredList = goals.where((goals) => goal.region == 'North America')
    

    【讨论】:

    • 如何在项目生成器中实现filteredList
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2020-09-11
    • 2020-08-20
    • 2021-07-24
    • 2023-03-22
    相关资源
    最近更新 更多