如何从API抖动中过滤特定数据?

时间:2020-07-29 07:48:36

标签: json api flutter filter

我有一个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中获取来自北美的数据

1 个答案:

答案 0 :(得分:0)

您可以使用where中的List功能

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