import 'package:bdk_flutter_quickstart/screens/home.dart';
import 'package:bdk_flutter_quickstart/styles/theme.dart';
-import 'package: flutter/material.dart';
+import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
late Blockchain blockchain;
TextEditingController mnemonic = TextEditingController();
- generateMnemonicHandler() async {
+ Future<void> generateMnemonicHandler() async {
var res = await Mnemonic.create(WordCount.Words12);
setState(() {
mnemonic.text = res.asString();
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 30),
child: Column(
- children: const [
+ children: [
/* Balance */
/* Result */
// modify the generateMnemonicHandler method to also set mnemonic as displayText
- generateMnemonicHandler() async {
+ Future<void> generateMnemonicHandler() async {
var res = await Mnemonic.create(WordCount.Words12);
setState(() {
mnemonic.text = res.asString();
network: Network.Testnet,
mnemonic: mnemonicObj,
);
- final secretKey = descriptorSecretKey.asString();
final descriptor = await Descriptor.newBip84(
- secretKey: secretKey, network: Network.Testnet, keyChainKind: e);
+ secretKey: descriptorSecretKey,
+ network: Network.Testnet,
+ keychain: e,
+ );
descriptors.add(descriptor);
}
return descriptors;
Following our pattern of a button, click handler and bdk-flutter API call, Let's add an internal method which will serve as the click handler for the "Create Wallet" button. We want to see the output of this call so let's use `setState()` to set the `wallet` object created and the `displayText` variable with the wallet's first receive address.
```dart
- createOrRestoreWallet(
- String mnemonic, Network network, String? password, String path) async {
+ Future<void> createOrRestoreWallet(
+ String mnemonic, Network network, String? password) async {
try {
final descriptors = await getDescriptors(mnemonic);
final res = await Wallet.create(
changeDescriptor: descriptors[1],
network: network,
databaseConfig: const DatabaseConfig.memory());
- var addressInfo = await res.getAddress(addressIndex: AddressIndex.New);
+ var addressInfo = await res.getAddress(addressIndex: const AddressIndex());
setState(() {
address = addressInfo.address;
wallet = res;
SubmitButton(
text: "Create Wallet",
callback: () async {
- final res = await createOrRestoreWallet(mnemonic.text, Network.TESTNET,
- "password");
- setState(() {
- displayText = "Wallet Created: ${wallet?.address ?? "Error"}";
- wallet = res;
- });
+ await createOrRestoreWallet(
+ mnemonic.text,
+ Network.Testnet,
+ "password",
+ );
},
),
```
Let's add an internal method to create and initialize the `Blockchain` object.
```dart
- blockchainInit() async {
+ Future<void> blockchainInit() async {
blockchain = await Blockchain.create(
config: BlockchainConfig.electrum(
config: ElectrumConfig(
Let's add two internal functions for syncing UTXOs and compute balance.
```dart
- getBalance() async {
+ Future<void> getBalance() async {
final balanceObj = await wallet.getBalance();
final res = "Total Balance: ${balanceObj.total.toString()}";
print(res);
});
}
- syncWallet() async {
+ Future<void> syncWallet() async {
wallet.sync(blockchain);
}
Add a new `getNewAddress` function below the `syncWallet()` function:
```dart
- getNewAddress() async {
- final res = await wallet.getAddress(addressIndex: AddressIndex.New);
+ Future<void> getNewAddress() async {
+ final res = await wallet.getAddress(addressIndex: const AddressIndex());
setState(() {
displayText = res.address;
address = res.address;
Let's make an internal function to send a bitcoin transaction, using `Wallet`, `Blockchain` and `TxBuilder `.
```dart
- sendTx(String addressStr, int amount) async {
+ Future<void> sendTx(String addressStr, int amount) async {
try {
final txBuilder = TxBuilder();
final address = await Address.create(address: addressStr);