lab 5 done
This commit is contained in:
parent
5daf361527
commit
0ca1277fb3
@ -2,31 +2,33 @@ import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'car_dto.g.dart';
|
||||
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class CarDto {
|
||||
final List<CarResultDto>? result;
|
||||
const CarDto({this.result});
|
||||
@JsonKey(name: 'Count')
|
||||
final int? count;
|
||||
@JsonKey(name: 'Message')
|
||||
final String? message;
|
||||
@JsonKey(name: 'SearchCriteria')
|
||||
final String? searchCriteria;
|
||||
@JsonKey(name: 'Results')
|
||||
final List<CarAttributesResultDto>? results;
|
||||
|
||||
const CarDto({this.count, this.message, this.searchCriteria, this.results});
|
||||
factory CarDto.fromJson(Map<String, dynamic> json) => _$CarDtoFromJson(json);
|
||||
}
|
||||
@JsonSerializable(createToJson: false)
|
||||
class CarResultDto {
|
||||
final String? id;
|
||||
final CarAttributesResultDto? attributes;
|
||||
|
||||
const CarResultDto({this.id, this.attributes});
|
||||
|
||||
factory CarResultDto.fromJson(Map<String, dynamic> json) => _$CarResultDtoFromJson(json);
|
||||
}
|
||||
@JsonSerializable(createToJson: false)
|
||||
class CarAttributesResultDto {
|
||||
@JsonKey(name: 'Model_ID')
|
||||
final String? modelid;
|
||||
final int? modelID;
|
||||
@JsonKey(name: 'Make_Name')
|
||||
final String? makename; // Поле для make_and_model
|
||||
final String? makeName;
|
||||
@JsonKey(name: 'Model_Name')
|
||||
final String? modelname;
|
||||
final String? modelName;
|
||||
|
||||
const CarAttributesResultDto({this.modelid, this.makename, this.modelname});
|
||||
const CarAttributesResultDto({ this.modelID, this.makeName, this.modelName});
|
||||
|
||||
factory CarAttributesResultDto.fromJson(Map<String, dynamic> json) => _$CarAttributesResultDtoFromJson(json);
|
||||
// Map<String, dynamic> toJson() => _$CarAttributesResultDtoFromJson(this);
|
||||
}
|
||||
|
||||
|
@ -1,29 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'car_dto.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
CarDto _$CarDtoFromJson(Map<String, dynamic> json) => CarDto(
|
||||
result: (json['result'] as List<dynamic>?)
|
||||
?.map((e) => CarResultDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
CarResultDto _$CarResultDtoFromJson(Map<String, dynamic> json) => CarResultDto(
|
||||
id: json['id'] as String?,
|
||||
attributes: json['attributes'] == null
|
||||
? null
|
||||
: CarAttributesResultDto.fromJson(
|
||||
json['attributes'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
CarAttributesResultDto _$CarAttributesResultDtoFromJson(
|
||||
Map<String, dynamic> json) =>
|
||||
CarAttributesResultDto(
|
||||
modelid: json['Model_ID'] as String?,
|
||||
makename: json['Make_Name'] as String?,
|
||||
modelname: json['Model_Name'] as String?,
|
||||
);
|
@ -1,13 +1,17 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mylab/data/dtos/car_dto.dart';
|
||||
import 'package:mylab/domain/models/car.dart';
|
||||
|
||||
const _imagePlaceholder =
|
||||
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
||||
'https://avatars.mds.yandex.net/i?id=d8136fe8562de785a003fa49a2a07a3d_l-4507854-images-thumbs&n=13';
|
||||
|
||||
extension CarDataDtoToModel on CarResultDto {
|
||||
extension CarAttributesResultDtoToModel on CarAttributesResultDto {
|
||||
CarData toDomain() => CarData(
|
||||
attributes?.modelid ?? 'UNKNOWN',
|
||||
imageUrl: _imagePlaceholder,
|
||||
descriptionCar: '${attributes?.makename}, ${attributes?.modelname}'
|
||||
modelName ?? 'unknown',
|
||||
descriptionCar: 'ID: $modelID \n'
|
||||
'Marka: $makeName \n'
|
||||
'Model: $modelName',
|
||||
|
||||
imageUrl: _imagePlaceholder
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -12,20 +12,25 @@ class CarRepository extends ApiInterface {
|
||||
requestBody: true,
|
||||
));
|
||||
|
||||
// https://random-data-api.com/api/vehicle/random_vehicle.json
|
||||
|
||||
static const String _baseUrl = 'https://vpic.nhtsa.dot.gov/api/vehicles';
|
||||
|
||||
@override
|
||||
Future<List<CarData>?> loadData({String? q, OnErrorCallback? onError}) async {
|
||||
Future<List<CarData>?> loadData({String? q, OnErrorCallback? onError, int limit = 20}) async {
|
||||
try {
|
||||
const String url = '$_baseUrl/GetModelsForMakeYear/make/honda/modelyear/1990?format=json';
|
||||
|
||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
|
||||
|
||||
print('Response data: ${response.data}');
|
||||
final CarDto dto = CarDto.fromJson(response.data as Map<String, dynamic>);
|
||||
final List<CarData>? data = dto.result?.map((e) => e.toDomain()).toList();
|
||||
final List<CarData>? data = dto.results?.map((e) => e.toDomain()).toList();
|
||||
// Ограничиваем количество возвращаемых записей до 'limit'
|
||||
if (data != null && data.length > limit) {
|
||||
return data.sublist(0, limit);
|
||||
}
|
||||
return data;
|
||||
} on DioException catch (e) {
|
||||
print(e.response?.statusMessage);
|
||||
onError?.call(e.response?.statusMessage);
|
||||
return null;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user