5try
This commit is contained in:
parent
89ff2e9a82
commit
6f93cfbbe7
29
lib/data/dtos/ stocks_dto.dart
Normal file
29
lib/data/dtos/ stocks_dto.dart
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:ffi';
|
||||||
|
|
||||||
|
import 'package:json_annotation/json_annotation.dart';
|
||||||
|
|
||||||
|
part 'stocks_dto.g.dart';
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class StocksDto {
|
||||||
|
final List<StockDataDto>? stocks;
|
||||||
|
|
||||||
|
const StocksDto({this.stocks});
|
||||||
|
|
||||||
|
factory StocksDto.fromJson(Map<String, dynamic> json) => _$StocksDtoFromJson(json);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class StockDataDto {
|
||||||
|
final int? fdcId;
|
||||||
|
final String? name;
|
||||||
|
final double? price;
|
||||||
|
final String? image;
|
||||||
|
|
||||||
|
const StockDataDto({this.fdcId, this.name, this.price, this.image});
|
||||||
|
|
||||||
|
factory StockDataDto.fromJson(Map<String, dynamic> json) => _$StockDataDtoFromJson(json);
|
||||||
|
}
|
20
lib/data/dtos/stocks_dto.g.dart
Normal file
20
lib/data/dtos/stocks_dto.g.dart
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'stocks_dto.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// JsonSerializableGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
StocksDto _$StocksDtoFromJson(Map<String, dynamic> json) => StocksDto(
|
||||||
|
foods: (json['stocks'] as List<dynamic>?)
|
||||||
|
?.map((e) => StockDataDto.fromJson(e as Map<String, dynamic>))
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
|
||||||
|
StockDataDto _$StockDataDtoFromJson(Map<String, dynamic> json) => StockDataDto(
|
||||||
|
fdcId: (json['fdcId'] as num?)?.toInt(),
|
||||||
|
name: json['name'] as String?,
|
||||||
|
price: json['price'] as double?,
|
||||||
|
image: json['image'] as String?,
|
||||||
|
);
|
11
lib/data/mappers/stock_mapper.dart
Normal file
11
lib/data/mappers/stock_mapper.dart
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
import 'package:flutter_project/data/dtos/stocks_dto.dart';
|
||||||
|
import 'package:flutter_project/presentation/home_page/home_page.dart';
|
||||||
|
|
||||||
|
|
||||||
|
extension CharacterDataDtoToModel on StockDataDto {
|
||||||
|
Stock toDomain() => Stock(
|
||||||
|
name: name ?? '',
|
||||||
|
price: price,
|
||||||
|
imageUrl: image);
|
||||||
|
}
|
27
lib/repositories/ mock_repository.dart
Normal file
27
lib/repositories/ mock_repository.dart
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import 'package:flutter_project/presentation/StockCategory.dart';
|
||||||
|
import 'package:flutter_project/presentation/home_page/home_page.dart';
|
||||||
|
import 'package:flutter_project/repositories/api_interface.dart';
|
||||||
|
|
||||||
|
class MockRepository extends ApiInterface {
|
||||||
|
@override
|
||||||
|
Future<List<Stock>?> loadData() async {
|
||||||
|
return [
|
||||||
|
Stock(
|
||||||
|
name: 'Алроса',
|
||||||
|
price: 52.90,
|
||||||
|
imageUrl:
|
||||||
|
'https://data.cbonds.info/organisations_logos/18/1617957662ALROSA_Rus_COLOUR_RGB.jpg',
|
||||||
|
category: StockCategory.mining),
|
||||||
|
Stock(
|
||||||
|
name: 'Газпром',
|
||||||
|
price: 135.431111,
|
||||||
|
imageUrl: 'https://data.cbonds.info/organisations_logos/21/21.png',
|
||||||
|
category: StockCategory.oilgas),
|
||||||
|
Stock(
|
||||||
|
name: 'Сбербанк',
|
||||||
|
imageUrl:
|
||||||
|
'https://data.cbonds.info/organisations_logos/313/1615279834sberbank__2-01.png',
|
||||||
|
category: StockCategory.finance),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
6
lib/repositories/api_interface.dart
Normal file
6
lib/repositories/api_interface.dart
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
import 'package:flutter_project/presentation/home_page/home_page.dart';
|
||||||
|
|
||||||
|
abstract class ApiInterface {
|
||||||
|
Future<List<Stock>?> loadData();
|
||||||
|
}
|
35
lib/repositories/stock_repository.dart
Normal file
35
lib/repositories/stock_repository.dart
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import 'package:flutter_project/presentation/home_page/home_page.dart';
|
||||||
|
import 'package:flutter_project/data/dtos/stocks_dto.dart';
|
||||||
|
import 'package:flutter_project/data/mappers/stock_mapper.dart';
|
||||||
|
import 'package:flutter_project/repositories/api_interface.dart';
|
||||||
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
|
||||||
|
|
||||||
|
class StockRepository extends ApiInterface {
|
||||||
|
static final Dio _dio = Dio()
|
||||||
|
..interceptors.add(
|
||||||
|
PrettyDioLogger(
|
||||||
|
requestHeader: true,
|
||||||
|
requestBody: true,
|
||||||
|
));
|
||||||
|
|
||||||
|
static const String _baseUrl = 'https://api.nal.usda.gov';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<Stock>?> loadData({String? q}) async { // или вместо stock - stockcard
|
||||||
|
try {
|
||||||
|
final String searchQuery = q ?? 'Stock'; // Используем 'Stock' как значение по умолчанию
|
||||||
|
final String url = '$_baseUrl/fdc/v1/foods/search?api_key=91xPcWwfSGljSRMuoS8IH0GP4hM9QqwwtgSzqJMw&query=$searchQuery&pageSize=10';
|
||||||
|
|
||||||
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
|
||||||
|
|
||||||
|
final StocksDto dto = StocksDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
|
final List<Stock>? data = dto.stocks?.map((e) => e.toDomain()).toList();
|
||||||
|
return data;
|
||||||
|
} on DioException catch (e) {
|
||||||
|
// err
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
64
pubspec.lock
64
pubspec.lock
@ -49,6 +49,22 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.8"
|
version: "1.0.8"
|
||||||
|
dio:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: dio
|
||||||
|
sha256: "5598aa796bbf4699afd5c67c0f5f6e2ed542afc956884b9cd58c306966efc260"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.7.0"
|
||||||
|
dio_web_adapter:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: dio_web_adapter
|
||||||
|
sha256: "33259a9276d6cea88774a0000cfae0d861003497755969c92faa223108620dc8"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.0"
|
||||||
equatable:
|
equatable:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -91,6 +107,22 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
http_parser:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: http_parser
|
||||||
|
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.0.2"
|
||||||
|
json_annotation:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: json_annotation
|
||||||
|
sha256: "1ce844379ca14835a50d2f019a3099f419082cfdd231cd86a142af94dd5c6bb1"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.9.0"
|
||||||
leak_tracker:
|
leak_tracker:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -155,6 +187,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.9.0"
|
version: "1.9.0"
|
||||||
|
pretty_dio_logger:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: pretty_dio_logger
|
||||||
|
sha256: "36f2101299786d567869493e2f5731de61ce130faa14679473b26905a92b6407"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.4.0"
|
||||||
sky_engine:
|
sky_engine:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description: flutter
|
description: flutter
|
||||||
@ -208,6 +248,22 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.7.2"
|
version: "0.7.2"
|
||||||
|
tinkoff_sdk:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: tinkoff_sdk
|
||||||
|
sha256: "20cc80ec6f57aa8ed499fb069075cc9d2b1361b369ea9159aad2c1663f54e191"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.6.0"
|
||||||
|
typed_data:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: typed_data
|
||||||
|
sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.4.0"
|
||||||
vector_math:
|
vector_math:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -224,6 +280,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "14.2.5"
|
version: "14.2.5"
|
||||||
|
web:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: web
|
||||||
|
sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.1.0"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=3.5.2 <4.0.0"
|
dart: ">=3.5.2 <4.0.0"
|
||||||
flutter: ">=3.18.0-18.0.pre.54"
|
flutter: ">=3.18.0-18.0.pre.54"
|
||||||
|
@ -31,7 +31,11 @@ dependencies:
|
|||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
|
||||||
|
tinkoff_sdk: ^0.6.0
|
||||||
fl_chart: ^0.69.0
|
fl_chart: ^0.69.0
|
||||||
|
json_annotation: ^4.8.1
|
||||||
|
dio: ^5.4.2+1
|
||||||
|
pretty_dio_logger: ^1.3.1
|
||||||
# The following adds the Cupertino Icons font to your application.
|
# The following adds the Cupertino Icons font to your application.
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^1.0.8
|
cupertino_icons: ^1.0.8
|
||||||
|
Loading…
x
Reference in New Issue
Block a user