Skip to content

Commit

Permalink
Add null handling to AccountState.fromJson
Browse files Browse the repository at this point in the history
Save BigInt's as string

Set isInitial default value to true
  • Loading branch information
erdemyerebasmaz committed Oct 31, 2024
1 parent 498e775 commit c0873ed
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lib/cubit/account/account_state.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:convert';

import 'package:breez_liquid/breez_liquid.dart';
import 'package:logging/logging.dart';

final _log = Logger("AccountState");

class AccountState {
final bool isInitial;
Expand Down Expand Up @@ -31,7 +34,7 @@ class AccountState {

factory AccountState.fromJson(Map<String, dynamic> json) {
return AccountState(
isInitial: json["isInitial"],
isInitial: json["isInitial"] ?? true,
walletInfo: GetInfoResponseFromJson.fromJson(json['walletInfo']),
);
}
Expand All @@ -43,17 +46,31 @@ class AccountState {
extension GetInfoResponseToJson on GetInfoResponse {
Map<String, dynamic> toJson() {
return {
'balanceSat': balanceSat,
'pendingSendSat': pendingSendSat,
'pendingReceiveSat': pendingReceiveSat,
'balanceSat': balanceSat.toString(),
'pendingSendSat': pendingSendSat.toString(),
'pendingReceiveSat': pendingReceiveSat.toString(),
'fingerprint': fingerprint,
'pubkey': pubkey,
};
}
}

extension GetInfoResponseFromJson on GetInfoResponse {
static GetInfoResponse fromJson(Map<String, dynamic> json) {
static GetInfoResponse? fromJson(Map<String, dynamic>? json) {
if (json == null) {
_log.info("walletInfo is missing from AccountState JSON.");
return null;
}

if (json['balanceSat'] == null ||
json['pendingSendSat'] == null ||
json['pendingReceiveSat'] == null ||
json['fingerprint'] == null ||
json['pubkey'] == null) {
_log.warning("GetInfoResponse has missing fields on AccountState JSON.");
return null;
}

return GetInfoResponse(
balanceSat: BigInt.parse(json['balanceSat'] as String),
pendingSendSat: BigInt.parse(json['pendingSendSat'] as String),
Expand Down

0 comments on commit c0873ed

Please sign in to comment.