diff --git a/cyberwow/lib/controller/rpc/rpc.dart b/cyberwow/lib/controller/rpc/rpc.dart index b66b1fa..ede1dfa 100644 --- a/cyberwow/lib/controller/rpc/rpc.dart +++ b/cyberwow/lib/controller/rpc/rpc.dart @@ -81,13 +81,14 @@ Future rpcString(String method, {String field}) async { Future syncInfo() => rpc('sync_info'); Future syncInfoString() => rpcString('sync_info'); -Future targetHeight() => rpc('sync_info', field: 'target_height').then((x) => x as int); -Future height() => rpc('sync_info', field: 'height').then((x) => x as int); +Future targetHeight() => rpc('sync_info', field: 'target_height').then(asInt); +Future height() => rpc('sync_info', field: 'height').then(asInt); Future getInfo() => rpc('get_info'); Future> getInfoSimple() async { - final Map _getInfo = await rpc('get_info'); + final _getInfo = await rpc('get_info').then(asMap); + return _getInfo.map ( (k, v) { @@ -102,17 +103,15 @@ Future> getInfoSimple() async { Future getInfoString() => rpcString('get_info'); -Future offline() => rpc('get_info', field: 'offline').then((x) => x as bool); +Future offline() => rpc('get_info', field: 'offline').then(asBool); Future outgoingConnectionsCount() => - rpc('get_info', field: 'outgoing_connections_count') - .then((x) => x as int); + rpc('get_info', field: 'outgoing_connections_count').then(asInt); Future incomingConnectionsCount() => - rpc('get_info', field: 'incoming_connections_count') - .then((x) => x as int); + rpc('get_info', field: 'incoming_connections_count').then(asInt); Future> getConnectionsSimple() async { - final List _connections = await rpc('get_connections', field: 'connections'); + final _connections = await rpc('get_connections', field: 'connections').then(asList); return _connections.map ( diff --git a/cyberwow/lib/helper.dart b/cyberwow/lib/helper.dart index b142be7..d6082e9 100644 --- a/cyberwow/lib/helper.dart +++ b/cyberwow/lib/helper.dart @@ -25,3 +25,12 @@ String pretty(dynamic x) { final JsonEncoder encoder = new JsonEncoder.withIndent(' '); return encoder.convert(x); } + + +int asInt(dynamic x) => x == null ? 0 : x; + +bool asBool(dynamic x) => x == null ? false : x; + +List asList(dynamic x) => x == null ? [] : x; + +Map asMap(dynamic x) => x == null ? {} : x;