API CALL

  • Screen 1

    main.dart

    
                 import 'package:flutter/material.dart';
    import 'package:b/filter_screen.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
    
          home: FilterScreen(),
          debugShowCheckedModeBanner: false,
        );
      }
    }
    
    

    services.dart

    
    
    import 'dart:convert';
    
    import 'package:b/model.dart';
    import 'package:http/http.dart' as http;
    
    class ApiServices {
      Future<FilterModel?> myFilterList() async {
        Uri url = Uri.parse("https://dummyjson.com/products");
        var response = await http.get(url);
        try {
          if (response.statusCode == 200) {
            return FilterModel.fromJson(jsonDecode(response.body));
          } else {
            return null;
          }
        } catch (e) {
          print(e);
        }
        return null;
      }
    }
    // the error message shown due to the rating value,
    // in some product  rating value in the form of double in some product it's value is iintn  form.
    
    

    model.dart

    
    
    class FilterModel {
      List<Products>? products;
      num? total;
      num? skip;
      num? limit;
    
      FilterModel({this.products, this.total, this.skip, this.limit});
    
      FilterModel.fromJson(Map<String, dynamic> json) {
        if (json['products'] != null) {
          products = <Products>[];
          json['products'].forEach((v) {
            products!.add(Products.fromJson(v));
          });
        }
        total = json['total'];
        skip = json['skip'];
        limit = json['limit'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = Map<String, dynamic>();
        if (products != null) {
          data['products'] = products!.map((v) => v.toJson()).toList();
        }
        data['total'] = total;
        data['skip'] = skip;
        data['limit'] = limit;
        return data;
      }
    }
    
    class Products {
      num? id;
      String? title;
      String? description;
      num? price;
      num? discountPercentage;
      dynamic rating;
      num? stock;
      String? brand;
      String? category;
      String? thumbnail;
      List<String>? images;
    
      Products(
          {this.id,
            this.title,
            this.description,
            this.price,
            this.discountPercentage,
            this.rating,
            this.stock,
            this.brand,
            this.category,
            this.thumbnail,
            this.images});
    
      Products.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        title = json['title'];
        description = json['description'];
        price = json['price'];
        discountPercentage = json['discountPercentage'];
        rating = json['rating'];
        stock = json['stock'];
        brand = json['brand'];
        category = json['category'];
        thumbnail = json['thumbnail'];
        images = json['images'].cast<String>();
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = Map<String, dynamic>();
        data['id'] = id;
        data['title'] = title;
        data['description'] = description;
        data['price'] = price;
        data['discountPercentage'] = discountPercentage;
        data['rating'] = rating;
        data['stock'] = stock;
        data['brand'] = brand;
        data['category'] = category;
        data['thumbnail'] = thumbnail;
        data['images'] = images;
        return data;
      }
    }
    
    

    filter_screen.dart

    
    
    import 'package:b/model.dart';
    import 'package:b/services.dart';
    import 'package:flutter/material.dart';
    
    class FilterScreen extends StatefulWidget {
      const FilterScreen({super.key});
    
      @override
      State<FilterScreen> createState() => _FilterScreenState();
    }
    
    class _FilterScreenState extends State<FilterScreen> {
      filterProduct() {
        ApiServices().myFilterList().then((value) {
          myItems(value!.products!);
          setState(() {});
        });
      }
    
      // for filtering the items
      List<Products> filterLists = [];
      List<Products> myItems(List<Products> list) {
        for (var element in list) {
          if (element.rating > 4.7 && element.rating < 5) {
            // show only the items name whose rating is greate then 4.7 and less then 5
            filterLists.add(element);
          }
        }
        return filterLists;
      }
    
      @override
      void initState() {
        filterProduct();
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text("Filter The Item's From API"),
          ),
          body: ListView.builder(
              shrinkWrap: true,
              itemCount: filterLists.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(
                    filterLists[index].title.toString(),
                  ),
                  subtitle: Text(
                    "Rating : ${filterLists[index].rating.toString()}",
                  ),
                );
              }),
        );
      }
    }
    
    // in ther it showt he product  title whose brand is apple  lets check it  from postman