skip to Main Content

I’m new to Flutter. I’m trying to learn. I’ve drawn a staff list with API, but I need to set a condition when pulling these staff, example: I want to attract staff whose name is George. I couldn’t use the where condition, here are my codes.

 final url = Uri.parse('https://reqres.in/api/users');
  int? counter;
  var personelResult;

  Future getPerson() async {
    try {
      final response = await http.get(url);
      if (response.statusCode == 200) {
        var result = personalFromJson(response.body);
        if (mounted) {
          setState(() {
            counter = result.data.length;
            personelResult = result;
    personelResult= personelResult.where((element) => element.firstName == "George").toList();

            
          });
        }
        return result;
      } else {
        print(response.statusCode);
      }
    } catch (e) {
      print(e.toString());
    }
  }

  @override
  void initState() {
    super.initState();
    getPerson();
    
  }

my codes get status = 200 in try catch and error in setState
Errors returned from Cacth:

Class 'Personal' has no instance method 'where'.
Receiver: Instance of 'Personal'
Tried calling: where(Closure: (dynamic) => bool)

my Buddha model

// To parse this JSON data, do
//
//     final personal = personalFromJson(jsonString);

import 'dart:convert';

Personal personalFromJson(String str) => Personal.fromJson(json.decode(str));

String personalToJson(Personal data) => json.encode(data.toJson());

class Personal {
  Personal({
   required this.page,
   required this.perPage,
   required this.total,
   required this.totalPages,
   required this.data,
    required this.support,
  });

  int page;
  int perPage;
  int total;
  int totalPages;
  List<Datum> data;
  Support support;

  factory Personal.fromJson(Map<String, dynamic> json) => Personal(
        page: json["page"],
        perPage: json["per_page"],
        total: json["total"],
        totalPages: json["total_pages"],
        data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
        support: Support.fromJson(json["support"]),
      );

  Map<String, dynamic> toJson() => {
        "page": page,
        "per_page": perPage,
        "total": total,
        "total_pages": totalPages,
        "data": List<dynamic>.from(data.map((x) => x.toJson())),
        "support": support.toJson(),
      };
}

class Datum {
  Datum({
   required this.id,
   required this.email,
   required this.firstName,
   required this.lastName,
    required this.avatar,
  });

  int id;
  String email;
  String firstName;
  String lastName;
  String avatar;

  factory Datum.fromJson(Map<String, dynamic> json) => Datum(
        id: json["id"],
        email: json["email"],
        firstName: json["first_name"],
        lastName: json["last_name"],
        avatar: json["avatar"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "email": email,
        "first_name": firstName,
        "last_name": lastName,
        "avatar": avatar,
      };
}

class Support {
  Support({
   required this.url,
  required  this.text,
  });

  String url;
  String text;

  factory Support.fromJson(Map<String, dynamic> json) => Support(
        url: json["url"],
        text: json["text"],
      );

  Map<String, dynamic> toJson() => {
        "url": url,
        "text": text,
      };
}

2

Answers


  1. Based on your model it is List<Datum> data; contains list. so the where method should be apply on data

     personelResult.data.where((element)=>...
    
    Login or Signup to reply.
  2. Iterable l = json.decode(response.body);
    List<Person> persons = List<Person>.from(l.map((model)=> Person.fromJson(model)));
    
    persons = persons.where((element) => element.firstName == "George").toList();
    

    If your response is a List of persons in json format, you can do it like this. But it looks like you return just a single person, thats why you can’t call .where on your result.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search