简体   繁体   中英

Dart Instance of the class name

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

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


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM