major refactor
This commit is contained in:
@@ -1,6 +1,31 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
int _asInt(dynamic value, [int fallback = 0]) {
|
||||
if (value is int) return value;
|
||||
if (value is num) return value.toInt();
|
||||
final parsed = int.tryParse(value?.toString() ?? '');
|
||||
return parsed ?? fallback;
|
||||
}
|
||||
|
||||
double _asDouble(dynamic value, [double fallback = 0]) {
|
||||
if (value is double) return value;
|
||||
if (value is num) return value.toDouble();
|
||||
final parsed = double.tryParse(value?.toString() ?? '');
|
||||
return parsed ?? fallback;
|
||||
}
|
||||
|
||||
String _asString(dynamic value, [String fallback = '']) {
|
||||
final str = value?.toString();
|
||||
return (str == null) ? fallback : str;
|
||||
}
|
||||
|
||||
DateTime _asDateTime(dynamic value, [DateTime? fallback]) {
|
||||
if (value is DateTime) return value;
|
||||
final parsed = DateTime.tryParse(value?.toString() ?? '');
|
||||
return parsed ?? (fallback ?? DateTime.fromMillisecondsSinceEpoch(0));
|
||||
}
|
||||
|
||||
class DestinationObject {
|
||||
const DestinationObject(
|
||||
this.label,
|
||||
@@ -92,8 +117,8 @@ class YearlyMileage {
|
||||
YearlyMileage({this.year, required this.mileage});
|
||||
|
||||
factory YearlyMileage.fromJson(Map<String, dynamic> json) => YearlyMileage(
|
||||
year: json['year'],
|
||||
mileage: (json['mileage'] as num).toDouble(),
|
||||
year: json['year'] is int ? json['year'] as int : int.tryParse('${json['year']}'),
|
||||
mileage: _asDouble(json['mileage']),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -341,10 +366,10 @@ class LeaderboardEntry {
|
||||
|
||||
factory LeaderboardEntry.fromJson(Map<String, dynamic> json) =>
|
||||
LeaderboardEntry(
|
||||
userId: json['user_id'],
|
||||
username: json['username'],
|
||||
userFullName: json['user_full_name'],
|
||||
mileage: (json['mileage'] as num).toDouble(),
|
||||
userId: _asString(json['user_id']),
|
||||
username: _asString(json['username']),
|
||||
userFullName: _asString(json['user_full_name']),
|
||||
mileage: _asDouble(json['mileage']),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -360,9 +385,9 @@ class TripSummary {
|
||||
});
|
||||
|
||||
factory TripSummary.fromJson(Map<String, dynamic> json) => TripSummary(
|
||||
tripId: json['trip_id'],
|
||||
tripName: json['trip_name'],
|
||||
tripMileage: (json['trip_mileage'] as num).toDouble(),
|
||||
tripId: _asInt(json['trip_id']),
|
||||
tripName: _asString(json['trip_name']),
|
||||
tripMileage: _asDouble(json['trip_mileage']),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -391,21 +416,22 @@ class Leg {
|
||||
});
|
||||
|
||||
factory Leg.fromJson(Map<String, dynamic> json) => Leg(
|
||||
id: json['leg_id'],
|
||||
tripId: json['leg_trip'] ?? 0,
|
||||
start: json['leg_start'],
|
||||
end: json['leg_end'],
|
||||
beginTime: DateTime.parse(json['leg_begin_time']),
|
||||
timezone: (json['leg_timezone'] as num).toInt(),
|
||||
network: json['leg_network'] ?? "",
|
||||
route: json['leg_route'],
|
||||
mileage: (json['leg_mileage'] as num).toDouble(),
|
||||
notes: json['leg_notes'] ?? "",
|
||||
headcode: json['leg_headcode'] ?? "",
|
||||
driving: json['leg_driving'],
|
||||
user: json['leg_user'],
|
||||
locos: (json['locos'] as List)
|
||||
.map((e) => Loco.fromJson(e as Map<String, dynamic>))
|
||||
id: _asInt(json['leg_id']),
|
||||
tripId: _asInt(json['leg_trip']),
|
||||
start: _asString(json['leg_start']),
|
||||
end: _asString(json['leg_end']),
|
||||
beginTime: _asDateTime(json['leg_begin_time']),
|
||||
timezone: _asInt(json['leg_timezone']),
|
||||
network: _asString(json['leg_network']),
|
||||
route: _asString(json['leg_route']),
|
||||
mileage: _asDouble(json['leg_mileage']),
|
||||
notes: _asString(json['leg_notes']),
|
||||
headcode: _asString(json['leg_headcode']),
|
||||
driving: _asInt(json['leg_driving']),
|
||||
user: _asString(json['leg_user']),
|
||||
locos: (json['locos'] is List ? (json['locos'] as List) : const [])
|
||||
.whereType<Map>()
|
||||
.map((e) => Loco.fromJson(Map<String, dynamic>.from(e)))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
@@ -458,10 +484,10 @@ class Station {
|
||||
});
|
||||
|
||||
factory Station.fromJson(Map<String, dynamic> json) => Station(
|
||||
id: json['id'],
|
||||
name: json['name'],
|
||||
network: json['network'],
|
||||
country: json['country'],
|
||||
id: _asInt(json['id']),
|
||||
name: _asString(json['name']),
|
||||
network: _asString(json['network']),
|
||||
country: _asString(json['country']),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user