Skip to content

Commit

Permalink
fix number parse problem
Browse files Browse the repository at this point in the history
  • Loading branch information
clssw1004 committed Dec 12, 2024
1 parent 2c31632 commit d727573
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 161 deletions.
5 changes: 3 additions & 2 deletions lib/data/http/http_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,11 @@ class HttpDataSource implements DataSource {

// 错误处理
Exception _handleDioError(DioException e) {
final path = e.requestOptions.path;
if (e.response?.data != null && e.response?.data is Map) {
return Exception(e.response?.data['message'] ?? e.message);
return Exception('$path: ${e.response?.data['message'] ?? e.message}');
}
return Exception(e.message);
return Exception('$path: ${e.message}');
}

@override
Expand Down
314 changes: 157 additions & 157 deletions lib/models/account_item.dart
Original file line number Diff line number Diff line change
@@ -1,157 +1,157 @@
import 'package:intl/intl.dart';

class AccountItemResponse {
final List<AccountItem> items;
final AccountSummary summary;

AccountItemResponse({
required this.items,
required this.summary,
});

factory AccountItemResponse.fromJson(Map<String, dynamic> json) =>
AccountItemResponse(
items: (json['items'] as List)
.map((item) => AccountItem.fromJson(item))
.toList(),
summary: AccountSummary.fromJson(json['summary']),
);
}

class AccountSummary {
final double allIn;
final double allOut;
final double allBalance;

AccountSummary({
required this.allIn,
required this.allOut,
required this.allBalance,
});

factory AccountSummary.fromJson(Map<String, dynamic> json) => AccountSummary(
allIn: (json['allIn'] as num).toDouble(),
allOut: (json['allOut'] as num).toDouble(),
allBalance: (json['allBalance'] as num).toDouble(),
);
}

class AccountItem {
final String id;
final String accountBookId;
final String type;
final double amount;
final String category;
final String? description;
final String? shop;
final String? fundId;
final String? fundName;
final DateTime accountDate;
final String? createdBy;
final String? updatedBy;
final DateTime? createdAt;
final DateTime? updatedAt;

AccountItem({
required this.id,
required this.accountBookId,
required this.type,
required this.amount,
required this.category,
this.description,
this.shop,
this.fundId,
this.fundName,
required this.accountDate,
this.createdBy,
this.updatedBy,
this.createdAt,
this.updatedAt,
});

Map<String, dynamic> toJson() => {
'id': id,
'accountBookId': accountBookId,
'type': type,
'amount': amount,
'category': category,
'description': description,
'shop': shop,
'fundId': fundId,
'fundName': fundName,
'accountDate': accountDate.toIso8601String(),
};

Map<String, dynamic> toJsonUpdate() => {
'id': id,
'accountBookId': accountBookId,
'type': type,
'amount': amount,
'category': category,
'description': description,
'shop': shop,
'fundId': fundId,
'accountDate': DateFormat('yyyy-MM-dd HH:mm:ss').format(accountDate),
};

Map<String, dynamic> toJsonCreate() => {
'accountBookId': accountBookId,
'type': type,
'amount': amount,
'category': category,
'description': description,
'shop': shop,
'fundId': fundId,
'accountDate': DateFormat('yyyy-MM-dd HH:mm:ss').format(accountDate),
};

factory AccountItem.fromJson(Map<String, dynamic> json) => AccountItem(
id: json['id'],
accountBookId: json['accountBookId'],
type: json['type'],
amount: json['amount'].toDouble(),
category: json['category'],
description: json['description'],
shop: json['shop'],
fundId: json['fundId'],
fundName: json['fundName'],
accountDate: DateTime.parse(json['accountDate']),
createdBy: json['createdBy'],
updatedBy: json['updatedBy'],
createdAt: json['createdAt'] != null
? DateTime.parse(json['createdAt'])
: null,
updatedAt: json['updatedAt'] != null
? DateTime.parse(json['updatedAt'])
: null,
);

AccountItem copyWith({
String? id,
String? accountBookId,
String? type,
double? amount,
String? category,
String? description,
String? shop,
String? fundId,
String? fundName,
DateTime? accountDate,
}) =>
AccountItem(
id: id ?? this.id,
accountBookId: accountBookId ?? this.accountBookId,
type: type ?? this.type,
amount: amount ?? this.amount,
category: category ?? this.category,
description: description ?? this.description,
shop: shop ?? this.shop,
fundId: fundId ?? this.fundId,
fundName: fundName ?? this.fundName,
accountDate: accountDate ?? this.accountDate,
createdBy: createdBy,
updatedBy: updatedBy,
createdAt: createdAt,
updatedAt: updatedAt,
);
}
import 'package:intl/intl.dart';

class AccountItemResponse {
final List<AccountItem> items;
final AccountSummary summary;

AccountItemResponse({
required this.items,
required this.summary,
});

factory AccountItemResponse.fromJson(Map<String, dynamic> json) =>
AccountItemResponse(
items: (json['items'] as List)
.map((item) => AccountItem.fromJson(item))
.toList(),
summary: AccountSummary.fromJson(json['summary']),
);
}

class AccountSummary {
final double allIn;
final double allOut;
final double allBalance;

AccountSummary({
required this.allIn,
required this.allOut,
required this.allBalance,
});

factory AccountSummary.fromJson(Map<String, dynamic> json) => AccountSummary(
allIn: (json['allIn'] as num).toDouble(),
allOut: (json['allOut'] as num).toDouble(),
allBalance: (json['allBalance'] as num).toDouble(),
);
}

class AccountItem {
final String id;
final String accountBookId;
final String type;
final double amount;
final String category;
final String? description;
final String? shop;
final String? fundId;
final String? fundName;
final DateTime accountDate;
final String? createdBy;
final String? updatedBy;
final DateTime? createdAt;
final DateTime? updatedAt;

AccountItem({
required this.id,
required this.accountBookId,
required this.type,
required this.amount,
required this.category,
this.description,
this.shop,
this.fundId,
this.fundName,
required this.accountDate,
this.createdBy,
this.updatedBy,
this.createdAt,
this.updatedAt,
});

Map<String, dynamic> toJson() => {
'id': id,
'accountBookId': accountBookId,
'type': type,
'amount': amount,
'category': category,
'description': description,
'shop': shop,
'fundId': fundId,
'fundName': fundName,
'accountDate': accountDate.toIso8601String(),
};

Map<String, dynamic> toJsonUpdate() => {
'id': id,
'accountBookId': accountBookId,
'type': type,
'amount': amount,
'category': category,
'description': description,
'shop': shop,
'fundId': fundId,
'accountDate': DateFormat('yyyy-MM-dd HH:mm:ss').format(accountDate),
};

Map<String, dynamic> toJsonCreate() => {
'accountBookId': accountBookId,
'type': type,
'amount': amount,
'category': category,
'description': description,
'shop': shop,
'fundId': fundId,
'accountDate': DateFormat('yyyy-MM-dd HH:mm:ss').format(accountDate),
};

factory AccountItem.fromJson(Map<String, dynamic> json) => AccountItem(
id: json['id'],
accountBookId: json['accountBookId'],
type: json['type'],
amount: (json['amount'] as num).toDouble(),
category: json['category'],
description: json['description'],
shop: json['shop'],
fundId: json['fundId'],
fundName: json['fundName'],
accountDate: DateTime.parse(json['accountDate']),
createdBy: json['createdBy'],
updatedBy: json['updatedBy'],
createdAt: json['createdAt'] != null
? DateTime.parse(json['createdAt'])
: null,
updatedAt: json['updatedAt'] != null
? DateTime.parse(json['updatedAt'])
: null,
);

AccountItem copyWith({
String? id,
String? accountBookId,
String? type,
double? amount,
String? category,
String? description,
String? shop,
String? fundId,
String? fundName,
DateTime? accountDate,
}) =>
AccountItem(
id: id ?? this.id,
accountBookId: accountBookId ?? this.accountBookId,
type: type ?? this.type,
amount: amount ?? this.amount,
category: category ?? this.category,
description: description ?? this.description,
shop: shop ?? this.shop,
fundId: fundId ?? this.fundId,
fundName: fundName ?? this.fundName,
accountDate: accountDate ?? this.accountDate,
createdBy: createdBy,
updatedBy: updatedBy,
createdAt: createdAt,
updatedAt: updatedAt,
);
}
14 changes: 12 additions & 2 deletions lib/pages/account_item/widgets/category_grid_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,24 @@ class CategoryGridSelector extends StatelessWidget {
}

List<Category> _getDisplayCategories(List<Category> allCategories) {
if (selectedCategory == null) {
if (selectedCategory == null || allCategories.isEmpty) {
return allCategories.take(_maxDisplayCount - 1).toList();
}

// 如果选中的分类不在前8个中,则替换最后一个
final selectedCategoryObj = allCategories.firstWhere(
(c) => c.name == selectedCategory,
orElse: () => allCategories.first,
orElse: () {
// 如果找不到选中的分类,创建一个新的分类对象
return Category(
id: '',
name: selectedCategory!,
accountBookId: '',
categoryType: '',
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
},
);

final displayCategories = allCategories.take(_maxDisplayCount - 1).toList();
Expand Down

0 comments on commit d727573

Please sign in to comment.