kursovaya with error

This commit is contained in:
elena 2024-12-21 14:59:36 +04:00
parent 646e2c8177
commit cea1545b9a
14 changed files with 169 additions and 164 deletions

3
devtools_options.yaml Normal file
View File

@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:

View File

@ -1,60 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
part 'characters_dto.g.dart';
@JsonSerializable(createToJson: false)
class CharactersDto {
final List<CharacterDataDto>? data;
final MetaDto? meta;
const CharactersDto({
this.data,
this.meta,
});
factory CharactersDto.fromJson(Map<String, dynamic> json) => _$CharactersDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class CharacterDataDto {
final String? id;
final String? type;
final CharacterAttributesDataDto? attributes;
const CharacterDataDto({this.id, this.type, this.attributes});
factory CharacterDataDto.fromJson(Map<String, dynamic> json) => _$CharacterDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class CharacterAttributesDataDto {
final String? name;
final String? born;
final String? died;
final String? image;
const CharacterAttributesDataDto({this.name, this.born, this.died, this.image});
factory CharacterAttributesDataDto.fromJson(Map<String, dynamic> json) =>
_$CharacterAttributesDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class MetaDto {
final PaginationDto? pagination;
const MetaDto({this.pagination});
factory MetaDto.fromJson(Map<String, dynamic> json) => _$MetaDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PaginationDto {
final int? current;
final int? next;
final int? last;
const PaginationDto({this.current, this.next, this.last});
factory PaginationDto.fromJson(Map<String, dynamic> json) => _$PaginationDtoFromJson(json);
}

View File

@ -1,42 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'characters_dto.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
CharactersDto _$CharactersDtoFromJson(Map<String, dynamic> json) => CharactersDto(
data: (json['data'] as List<dynamic>?)
?.map((e) => CharacterDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
meta: json['meta'] == null ? null : MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
);
CharacterDataDto _$CharacterDataDtoFromJson(Map<String, dynamic> json) => CharacterDataDto(
id: json['id'] as String?,
type: json['type'] as String?,
attributes: json['attributes'] == null
? null
: CharacterAttributesDataDto.fromJson(json['attributes'] as Map<String, dynamic>),
);
CharacterAttributesDataDto _$CharacterAttributesDataDtoFromJson(Map<String, dynamic> json) =>
CharacterAttributesDataDto(
name: json['name'] as String?,
born: json['born'] as String?,
died: json['died'] as String?,
image: json['image'] as String?,
);
MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
pagination: json['pagination'] == null
? null
: PaginationDto.fromJson(json['pagination'] as Map<String, dynamic>),
);
PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) => PaginationDto(
current: json['current'] as int?,
next: json['next'] as int?,
last: json['last'] as int?,
);

View File

@ -0,0 +1,60 @@
import 'package:json_annotation/json_annotation.dart';
part 'games_dto.g.dart';
@JsonSerializable(createToJson: false)
class GamesDto {
final List<GameDataDto>? data;
final MetaDto? meta;
const GamesDto({
this.data,
this.meta,
});
factory GamesDto.fromJson(Map<String, dynamic> json) => _$GamesDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class GameDataDto {
final String? id;
final List<GameAttributesDataDto>? attributes;
const GameDataDto({this.id, this.attributes});
factory GameDataDto.fromJson(Map<String, dynamic> json) => _$GameDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class GameAttributesDataDto {
final String? alias;
final String? descriptionShort;
final String? description;
final String? photoUrl;
final String? year;
const GameAttributesDataDto({this.alias, this.descriptionShort, this.description, this.photoUrl, this.year});
factory GameAttributesDataDto.fromJson(Map<String, dynamic> json) =>
_$GameAttributesDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class MetaDto {
final PaginationDto? pagination;
const MetaDto({this.pagination});
factory MetaDto.fromJson(Map<String, dynamic> json) => _$MetaDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PaginationDto {
final int? current;
final int? next;
final int? last;
const PaginationDto({this.current, this.next, this.last});
factory PaginationDto.fromJson(Map<String, dynamic> json) => _$PaginationDtoFromJson(json);
}

View File

@ -0,0 +1,47 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'games_dto.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
GamesDto _$GamesDtoFromJson(Map<String, dynamic> json) => GamesDto(
data: (json['data'] as List<dynamic>?)
?.map((e) => GameDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
meta: json['meta'] == null
? null
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
);
GameDataDto _$GameDataDtoFromJson(Map<String, dynamic> json) => GameDataDto(
id: json['id'] as String?,
attributes: (json['attributes'] as List<dynamic>?)
?.map(
(e) => GameAttributesDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
);
GameAttributesDataDto _$GameAttributesDataDtoFromJson(
Map<String, dynamic> json) =>
GameAttributesDataDto(
alias: json['alias'] as String?,
descriptionShort: json['descriptionShort'] as String?,
description: json['description'] as String?,
photoUrl: json['photoUrl'] as String?,
year: json['year'] as String?,
);
MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
pagination: json['pagination'] == null
? null
: PaginationDto.fromJson(json['pagination'] as Map<String, dynamic>),
);
PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
PaginationDto(
current: (json['current'] as num?)?.toInt(),
next: (json['next'] as num?)?.toInt(),
last: (json['last'] as num?)?.toInt(),
);

View File

@ -1,32 +0,0 @@
import 'package:flutter_test_app/data/dtos/characters_dto.dart';
import 'package:flutter_test_app/domain/models/card.dart';
import 'package:flutter_test_app/domain/models/home.dart';
const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
extension CharactersDtoToModel on CharactersDto {
HomeData toDomain() => HomeData(
data: data?.map((e) => e.toDomain()).toList(),
nextPage: meta?.pagination?.next,
);
}
extension CharacterDataDtoToModel on CharacterDataDto {
CardData toDomain() => CardData(
attributes?.name ?? 'UNKNOWN',
imageUrl: attributes?.image ?? _imagePlaceholder,
descriptionText: _makeDescriptionText(attributes?.born, attributes?.died),
id: id,
);
String _makeDescriptionText(String? born, String? died) {
return born != null && died != null
? '$born - $died'
: born != null
? 'born: $born'
: died != null
? 'died: $died'
: '';
}
}

View File

@ -0,0 +1,26 @@
import 'package:flutter_test_app/data/dtos/games_dto.dart';
import 'package:flutter_test_app/domain/models/card.dart';
import 'package:flutter_test_app/domain/models/home.dart';
const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
extension GamesDtoToModel on GamesDto {
HomeData toDomain() => HomeData(
data: data?.map((e) => e.toDomain()).toList(),
nextPage: meta?.pagination?.next,
);
}
extension GameDataDtoToModel on GameDataDto {
CardData toDomain() => CardData(
attributes?.alias ?? 'UNKNOWN',
imageUrl: attributes?.photoUrl ?? _imagePlaceholder,
descriptionText: _makeDescriptionText(attributes?.descriptionShort, attributes?.description, attributes?.year),
id: id,
);
String _makeDescriptionText(String? descriptionShort, String? description, String? year) {
return 'Год издания: $year. Описание: $descriptionShort';
}
}

View File

@ -1,39 +1,38 @@
import 'package:dio/dio.dart';
import 'package:flutter_test_app/data/dtos/characters_dto.dart';
import 'package:flutter_test_app/data/mappers/characters_mapper.dart';
import 'package:flutter_test_app/data/dtos/games_dto.dart';
import 'package:flutter_test_app/data/mappers/games_mapper.dart';
import 'package:flutter_test_app/data/repositories/api_interface.dart';
import 'package:flutter_test_app/domain/models/home.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
class PotterRepository extends ApiInterface {
class GameRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
static const String _baseUrl = 'https://api.potterdb.com';
static const String _baseUrl = 'https://api.tesera.ru/';
@override
Future<HomeData?> loadData({
OnErrorCallback? onError,
String? q,
String? alias,
int page = 1,
int pageSize = 25,
}) async {
try {
const String url = '$_baseUrl/v1/characters';
const String url = '$_baseUrl/games';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: {
'filter[name_cont]': q,
'page[number]': page,
'page[size]': pageSize,
'offset': page,
'limit': pageSize,
},
);
final CharactersDto dto = CharactersDto.fromJson(response.data as Map<String, dynamic>);
final GamesDto dto = GamesDto.fromJson(response.data as Map<String, dynamic>);
final HomeData data = dto.toDomain();
return data;
} on DioException catch (e) {

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_test_app/lib/data/dtos/games_dto.dart';
class CardData {
final String text;

View File

@ -3,7 +3,7 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test_app/components/locale/l10n/app_locale.dart';
import 'package:flutter_test_app/data/repositories/potter_repository.dart';
import 'package:flutter_test_app/data/repositories/game_repository.dart';
import 'package:flutter_test_app/presentation/home_page/bloc/bloc.dart';
import 'package:flutter_test_app/presentation/home_page/home_page.dart';
import 'package:flutter_test_app/presentation/like_bloc/like_bloc.dart';
@ -34,15 +34,15 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.orangeAccent),
useMaterial3: true,
),
home: RepositoryProvider<PotterRepository>(
home: RepositoryProvider<GameRepository>(
lazy: true,
create: (_) => PotterRepository(),
create: (_) => GameRepository(),
child: BlocProvider<LikeBloc>(
lazy: false,
create: (context) => LikeBloc(),
child: BlocProvider<HomeBloc>(
lazy: false,
create: (context) => HomeBloc(context.read<PotterRepository>()),
create: (context) => HomeBloc(context.read<GameRepository>()),
child: const HomePage(),
),
),

View File

@ -1,10 +1,10 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test_app/data/repositories/potter_repository.dart';
import 'package:flutter_test_app/data/repositories/game_repository.dart';
import 'package:flutter_test_app/presentation/home_page/bloc/events.dart';
import 'package:flutter_test_app/presentation/home_page/bloc/state.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState> {
final PotterRepository repo;
final GameRepository repo;
HomeBloc(this.repo) : super(const HomeState()) {
on<HomeLoadDataEvent>(_onLoadData);
@ -20,7 +20,7 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
String? error;
final data = await repo.loadData(
q: event.search,
alias: event.search,
page: event.nextPage ?? 1,
onError: (e) => error = e,
);

View File

@ -65,20 +65,21 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
return HomeState(
data: data == const $CopyWithPlaceholder()
? _value.data
// ignore: cast_nullable_to_non_nullable
// ignore: cast_nullable_to_non_nullable
: data as HomeData?,
isLoading: isLoading == const $CopyWithPlaceholder() || isLoading == null
? _value.isLoading
// ignore: cast_nullable_to_non_nullable
// ignore: cast_nullable_to_non_nullable
: isLoading as bool,
isPaginationLoading:
isPaginationLoading == const $CopyWithPlaceholder() || isPaginationLoading == null
? _value.isPaginationLoading
// ignore: cast_nullable_to_non_nullable
: isPaginationLoading as bool,
isPaginationLoading == const $CopyWithPlaceholder() ||
isPaginationLoading == null
? _value.isPaginationLoading
// ignore: cast_nullable_to_non_nullable
: isPaginationLoading as bool,
error: error == const $CopyWithPlaceholder()
? _value.error
// ignore: cast_nullable_to_non_nullable
// ignore: cast_nullable_to_non_nullable
: error as String?,
);
}

View File

@ -43,7 +43,7 @@ class _$LikeStateCWProxyImpl implements _$LikeStateCWProxy {
return LikeState(
likedIds: likedIds == const $CopyWithPlaceholder()
? _value.likedIds
// ignore: cast_nullable_to_non_nullable
// ignore: cast_nullable_to_non_nullable
: likedIds as List<String>?,
);
}

View File

@ -27,7 +27,8 @@ class _$LocaleStateCWProxyImpl implements _$LocaleStateCWProxy {
final LocaleState _value;
@override
LocaleState currentLocale(Locale currentLocale) => this(currentLocale: currentLocale);
LocaleState currentLocale(Locale currentLocale) =>
this(currentLocale: currentLocale);
@override
@ -41,10 +42,11 @@ class _$LocaleStateCWProxyImpl implements _$LocaleStateCWProxy {
Object? currentLocale = const $CopyWithPlaceholder(),
}) {
return LocaleState(
currentLocale: currentLocale == const $CopyWithPlaceholder() || currentLocale == null
? _value.currentLocale
// ignore: cast_nullable_to_non_nullable
: currentLocale as Locale,
currentLocale:
currentLocale == const $CopyWithPlaceholder() || currentLocale == null
? _value.currentLocale
// ignore: cast_nullable_to_non_nullable
: currentLocale as Locale,
);
}
}