Files

107 lines
3.0 KiB
Dart

import 'package:equatable/equatable.dart';
class AssetModel extends Equatable {
final String isin;
final String symbol;
final String name;
final double currentPrice;
final String currency;
final String exchange;
final List<String> exchanges;
final List<AssetTickerOption> tickers;
final String image;
const AssetModel({
required this.isin,
required this.symbol,
required this.name,
this.currentPrice = 0.0,
required this.currency,
required this.exchange,
required this.exchanges,
required this.tickers,
required this.image,
});
factory AssetModel.fromJson(Map<String, dynamic> json) {
double parseDouble(dynamic val) {
if (val == null) return 0.0;
if (val is num) return val.toDouble();
return double.tryParse(val.toString()) ?? 0.0;
}
return AssetModel(
isin: json['isin']?.toString() ?? '',
symbol: json['symbol']?.toString() ?? '',
name: json['name']?.toString() ?? '',
currentPrice: parseDouble(json['price'] ?? json['currentPrice']),
currency: json['currency']?.toString() ?? 'EUR',
exchange: json['exchange']?.toString() ?? 'XETRA',
exchanges: (json['exchanges'] as List?)?.map((e) => e.toString()).toList() ?? [],
tickers: (json['tickers'] as List?)
?.map((t) => AssetTickerOption.fromJson(t))
.toList() ??
[],
image: json['image']?.toString() ?? '',
);
}
Map<String, dynamic> toJson() {
return {
'isin': isin,
'symbol': symbol,
'name': name,
'currentPrice': currentPrice,
'currency': currency,
'exchange': exchange,
'exchanges': exchanges,
'tickers': tickers.map((t) => t.toJson()).toList(),
'image': image,
};
}
@override
List<Object?> get props => [isin, symbol, name, currentPrice, currency, exchange, exchanges, tickers, image];
}
class AssetTickerOption extends Equatable {
final String ticker;
final String exchange;
final String tradingCurrency;
final double currentPrice;
const AssetTickerOption({
required this.ticker,
required this.exchange,
required this.tradingCurrency,
required this.currentPrice,
});
factory AssetTickerOption.fromJson(Map<String, dynamic> json) {
double parseDouble(dynamic val) {
if (val == null) return 0.0;
if (val is num) return val.toDouble();
return double.tryParse(val.toString()) ?? 0.0;
}
return AssetTickerOption(
ticker: json['ticker']?.toString() ?? '',
exchange: json['exchange']?.toString() ?? 'XETRA',
tradingCurrency: json['tradingCurrency']?.toString() ?? json['currency']?.toString() ?? 'EUR',
currentPrice: parseDouble(json['currentPrice'] ?? json['price']),
);
}
Map<String, dynamic> toJson() {
return {
'ticker': ticker,
'exchange': exchange,
'tradingCurrency': tradingCurrency,
'currentPrice': currentPrice,
};
}
@override
List<Object?> get props => [ticker, exchange, tradingCurrency, currentPrice];
}