gpt4 book ai didi

Flutter - Retrofit Fetch Data Exception(颤动-改装提取数据异常)

转载 作者:bug小助手 更新时间:2023-10-28 13:06:04 30 4
gpt4 key购买 nike



I just started learning Rest Api and Retrofit package. I am creating HotelApp for practice and i need to get data models list from Api.

我刚开始学习Rest Api和Retrofit包。我创建的实践HotelApp,我需要从API获取数据模型列表。


I think i did everything as needed, but terminal throws an exception "Error has occured", it means no data has recieved from API Model. I guess i did something wrong when i was creating a models.

我想我已经按要求做了所有的事情,但是终端抛出了一个异常:Error Have Are Are,这意味着没有从API Model接收到数据。我想我在做模特的时候做错了什么。


At first i made an ApiService to get data from my API.

首先,我创建了一个ApiService来从我的API获取数据。


part 'api_service.g.dart';

// API URL
@RestApi(baseUrl: 'https://run.mocky.io/v3/35e0d18e-2521-4f1b-a575-f0fe366f66e3/')

abstract class ApiService {
factory ApiService(Dio dio) = _ApiService;

@GET('/hotels')
Future<List<HotelModel>> getHotels();
}

Api Data Model. I used quicktype.io, so maybe it made a mistake when it was converting api into dart code.

API数据模型。我使用的是icktype.io,所以在将API转换为DART代码时可能会出错。


@JsonSerializable()
class HotelModel {
int id;
String name;
String adress;
int minimalPrice;
String priceForIt;
int rating;
String ratingName;
List<String> imageUrls;
AboutTheHotel aboutTheHotel;

HotelModel({
required this.id,
required this.name,
required this.adress,
required this.minimalPrice,
required this.priceForIt,
required this.rating,
required this.ratingName,
required this.imageUrls,
required this.aboutTheHotel,
});

factory HotelModel.fromJson(Map<String, dynamic> json) => _$HotelModelFromJson(json);
Map<String, dynamic> toJson() => _$HotelModelToJson(this);
}

@JsonSerializable()
class AboutTheHotel {
String description;
List<String> peculiarities;

AboutTheHotel({
required this.description,
required this.peculiarities,
});

factory AboutTheHotel.fromJson(Map<String, dynamic> json) => _$AboutTheHotelFromJson(json);

// AboutTheHotel(
// description: json["description"],
// peculiarities: List<String>.from(json["peculiarities"].map((x) => x)),
// );

Map<String, dynamic> toJson() => _$AboutTheHotelToJson(this);
// {
// "description": description,
// "peculiarities": List<dynamic>.from(peculiarities.map((x) => x)),
// };
}

FutureBuilder created inside body parameter inside Scaffold. It throws an exception inside if hasData block.

在脚手架内创建了内部实体参数FutureBuilder。它在if hasData块内部抛出异常。


FutureBuilder _body() {
final apiService =
ApiService(Dio(BaseOptions(contentType: "application/json")));
return FutureBuilder(
future: apiService.getHotels(),
builder: (context, snapshots) {
if (snapshots.connectionState == ConnectionState.done) {
if (snapshots.hasData) { // exception happens here
final List<HotelModel> hotels = snapshots.data!;
return _hotels(hotels);
} else {
return const Center(
child: Text(
"Error has occurred"),
);
}
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
);
}

更多回答
优秀答案推荐
更多回答

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com