cardano_wallet_sdk icon indicating copy to clipboard operation
cardano_wallet_sdk copied to clipboard

Building a wallet not async

Open rok5ek opened this issue 3 years ago • 1 comments

Hi when I build the wallet the whole app UI freezes. Can you fix the wallet build method so it will be async?

rok5ek avatar Apr 23 '22 05:04 rok5ek

There are two ways to update a wallet. You can build the wallet and sync it with the blockchain in one call:

Result<Wallet, String> result = await walletBuilder.buildAndSync();
if (result.isOk()) {
    var wallet = result.unwrap();
    print("${wallet.walletName}: ${wallet.balance}");
}   

or you can do it in two steps. First call build() which returns an empty wallet immediately. Then call update() to sync with the blockchain which is an async call:

Result<Wallet, String> result = walletBuilder.build();
Wallet wallet = result.unwrap();
Coin oldBalance = wallet.balance;
var result2 = await wallet.update();
result2.when(
    ok: (_) => print("old:$oldBalance lovelace, new: ${wallet.balance} lovelace"),
    err: (message) => print("Error: $message"),
);  

For better performance, you could wrap the update() call in an isolate following the pattern outlined here: https://github.com/dart-lang/samples/blob/master/isolates/bin/send_and_receive.dart

reaster avatar Apr 23 '22 21:04 reaster