flutter 接收信息时出现问题/发生异常TypeError

pcrecxhr  于 2023-04-22  发布在  Flutter
关注(0)|答案(1)|浏览(144)

我想从API中读取我的信息。但面对这个错误。

这是我写的代码:

import '../models/LocationListApi.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;

class Network {
  static Uri url = Uri.parse(
      "http://pelak.puladin.com/laravel-api/api/v1/places?category_id=11");

  static List<PlacesEntity> location = [];

  static void getData() async {
    location.clear();
    var response = await http.get(url);
    var jsonDecode = convert.jsonDecode(response.body);

    var finalResponse = PlacesEntity.fromJson(jsonDecode['data']);

    for (var json in jsonDecode) {
      location.add(PlacesEntity.fromJson(json));
    }

    print(jsonDecode);
  }
}

我基于Jsonis制作的显示信息的模型如下所示:我的型号:

// ignore_for_file: file_names

class PlacesEntity {
  List<Place> places;

  PlacesEntity({
    required this.places,
  });

  factory PlacesEntity.fromJson(Map<String, dynamic> json) {
    return PlacesEntity(
      places: List<Place>.from(json['places'].map((x) => Place.fromJson(x))),
    );
  }
}

class Place {
  int id;
  int pelak;
  String name;
  String intro;
  String description;
  String latitude;
  String longitude;
  String address;
  String phone;
  String email;
  String website;
  String instagram;
  String image;
  String category;
  String ads;
  String adsLink;

  Place({
    required this.id,
    required this.pelak,
    required this.name,
    required this.intro,
    required this.description,
    required this.latitude,
    required this.longitude,
    required this.address,
    required this.phone,
    required this.email,
    required this.website,
    required this.instagram,
    required this.image,
    required this.category,
    required this.ads,
    required this.adsLink,
  });

  factory Place.fromJson(Map<String, dynamic> json) {
    return Place(
      id: json['id'],
      pelak: json['pelak'],
      name: json['name'],
      intro: json['intro'],
      description: json['description'],
      latitude: json['latitude'],
      longitude: json['longitude'],
      address: json['address'],
      phone: json['phone'],
      email: json['email'],
      website: json['website'],
      instagram: json['instagram'],
      image: json['image'],
      category: json['category'],
      ads: json['ads'],
      adsLink: json['ads_link'],
    );
  }
}

class MyJson {
  PlacesEntity data;
  int status;

  MyJson({
    required this.data,
    required this.status,
  });

  factory MyJson.fromJson(Map<String, dynamic> json) {
    return MyJson(
      data: PlacesEntity.fromJson(json['data']),
      status: json['status'],
    );
  }
}

我试着把它做成一个列表,我可以在应用程序中阅读,但我不能。问题可能是我的模型。无论如何,这是我出现的问题。如果你能帮助我,我将不胜感激

4nkexdtk

4nkexdtk1#

class ApiClass{
  Future<List<Place>> fetchUsers()async{

    final response = await http.get(Uri.parse("http://pelak.puladin.com/laravel-api/api/v1/places?category_id=11"));

    List listData = jsonDecode(response.body)["data"]["places"];

    if(response.statusCode == 200){
      var result = listData.map((e) => Place.fromJson(e)).toList();
      for(int i = 0; i<result.length; i++){
        print(result[i].name);
      }
      return result;
    }else{
      throw Exception("Error");
    }
  }
}

现在,为了访问它,创建一个Network类的示例和一个后期初始化的List。

final Network networkObject = Network();
late List<Place> placeList;

创建一个新的async方法并在initState中调用它:

void apiCall()async{
    placeList = await networkObject.getData();
}

相关问题