Oreofe Solarin
Oreofe Solarin

Reputation: 354

How to filter specific data from an API flutter?

I have an API https://sdgfortb.herokuapp.com/onetreeplanted

This is how I fetch data.

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();
} 

This is my model

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,
    );
  }
}

Fetching Data class Loading extends StatelessWidget { final String destination;

  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],
                              ),
                            ),
                          );
                        },

I want to fetch the array which includes the region as North America.

There are three other regions but I want to get data from North America from the API

Upvotes: 0

Views: 4024

Answers (1)

arthankamal
arthankamal

Reputation: 6413

You can use where function from List

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

Upvotes: 1

Related Questions