Home > Software engineering >  Dart Instance of the class name
Dart Instance of the class name

Time:12-17

i have "data" from from api but i keep geting instance of the class name, which i did not know where the error is coming from..

here is the code

getalRegion() async {
    final pref = await SharedPreferences.getInstance();
    final autorization = pref.getString("jwt") ?? "";
    try {
      var url = Uri.parse("https://sss.com/api/Region/Regions");
      Response response = await http.get(url, headers: {
        "Authorization": "Bearer $autorization",
      });
      if (response.statusCode == 200) {
        final List<Map<String, dynamic>> result =
            List<Map<String, dynamic>>.from(
                jsonDecode(utf8.decode(response.bodyBytes)));
        final getResult = result.map((e) => RegoinDetails.fromJson(e)).toList();
        freshRegion = getResult;
        print(freshRegion);
      }
    } catch (e) {
      log("$e");
    }
  }



[Instance of 'RegoinDetails', Instance of 'RegoinDetails', Instance of 'RegoinDetails', Instance of 'RegoinDetails', Instance of 'RegoinDetails', Instance of 'RegoinDetails', Instance of 'RegoinDetails', Instance of 'RegoinDetails', Instance of 'RegoinDetails', Instance of 'RegoinDetails']

here is the class

import 'dart:convert';

List<RegoinDetails> regoinDetailsFromJson(String str) =>
    List<RegoinDetails>.from(
        json.decode(str).map((x) => RegoinDetails.fromJson(x)));

String regoinDetailsToJson(List<RegoinDetails> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class RegoinDetails {
  RegoinDetails({
    required this.regionCode,
    required this.regionName,
  });

  String regionCode;
  String regionName;

  factory RegoinDetails.fromJson(Map<String, dynamic> json) => RegoinDetails(
        regionCode: json["regionCode"],
        regionName: json["regionName"],
      );

  Map<String, dynamic> toJson() => {
        "regionCode": regionCode,
        "regionName": regionName,
      };
}

CodePudding user response:

u will have to override toString in the RegoinDetails model with the properties that u need to see on the output

class RegoinDetails {
  RegoinDetails({
    required this.regionCode,
    required this.regionName,
  });

  String regionCode;
  String regionName;

  factory RegoinDetails.fromJson(Map<String, dynamic> json) => RegoinDetails(
        regionCode: json["regionCode"],
        regionName: json["regionName"],
      );

  Map<String, dynamic> toJson() => {
        "regionCode": regionCode,
        "regionName": regionName,
      };
  @override
  String toString() {
    return 'RegoinDetails(regionCode: $regionCode, regionName: $regionName )';
  }
}


  • Related