Polkadart
Polkadart is the most comprehensive Dart/Flutter SDK for interacting with Polkadot, Substrate, and other compatible blockchain networks. Designed with a Dart-first approach and type-safe APIs, it provides everything developers need to build powerful decentralized applications.
This page will outline some of the core components of Polkadart. For more details, refer to the official documentation.
Installation
Section titled “Installation”Add Polkadart to your pubspec.yaml:
dart pub add polkadart polkadart_cli polkadart_keyring polkadart_scale_codec secp256k1_ecdsa sr25519 ss58 substrate_bip39 substrate_metadatadart pub add polkadart polkadart_cli polkadart_keyringFor type-safe API generation, add the following to your pubspec.yaml:
polkadart: output_dir: lib/generated chains: polkadot: wss://rpc.polkadot.io kusama: wss://kusama-rpc.polkadot.io custom: wss://your-node.example.comGet Started
Section titled “Get Started”Type Generation
Section titled “Type Generation”Polkadart provides a CLI tool to generate type definitions from any Polkadot-SDK compatible blockchain network. This allows you to build type-safe Dart applications without dealing with the low-level details of the blockchain.
Run Generator
Section titled “Run Generator”dart run polkadart_cli:generate -vUse Generated Types
Section titled “Use Generated Types”import 'package:your_app/generated/polkadot/polkadot.dart';import 'package:polkadart/polkadart.dart';import 'package:ss58/ss58.dart';
final provider = Provider.fromUri(Uri.parse('wss://rpc.polkadot.io'));final polkadot = Polkadot(provider);
// Account from SS58 addressfinal account = Address.decode('19t9Q2ay58hMDaeg6eeBhqmHsRnc2jDMV3cYYw9zbc59HLj');
// Retrieve Account Balancefinal accountInfo = await polkadot.query.system.account(account.pubkey);print('Balance: ${accountInfo.data.free}')Creating an API Instance
Section titled “Creating an API Instance”An API instance is required to interact with the blockchain. Polkadart provides a Provider class that allows you to connect to any network.
import 'package:demo/generated/polkadot/polkadot.dart';import 'package:polkadart/provider.dart';
Future<void> main(List<String> arguments) async { final provider = Provider.fromUri(Uri.parse('wss://rpc.polkadot.io')); final polkadot = Polkadot(provider);}Reading Chain Data
Section titled “Reading Chain Data”Besides querying the data using the query from the generated API, you can also use the State API for querying storage data, metadata, runtime information, and other chain information.
final stateApi = StateApi(provider);
// Get current runtime versionfinal runtimeVersion = await stateApi.getRuntimeVersion();print(runtimeVersion.toJson());
// Get metadatafinal metadata = await stateApi.getMetadata();print('Metadata version: ${metadata.version}');Subscribe to New Blocks
Section titled “Subscribe to New Blocks”You can subscribe to new blocks on the blockchain using the subscribe method.
final subscription = await provider.subscribe('chain_subscribeNewHeads', []);
subscription.stream.forEach((response) { print('New head: ${response.result}');});Send a Transaction
Section titled “Send a Transaction”Perhaps the most common operation done in any blockchain is transferring funds. Here you can see how that can be done using Polkadart:
final wallet = await KeyPair.sr25519.fromUri("//Alice");print('Alice\' wallet: ${wallet.address}');
// Get information necessary to build a proper extrinsicfinal runtimeVersion = await polkadot.rpc.state.getRuntimeVersion();final currentBlockNumber = (await polkadot.query.system.number()) - 1;final currentBlockHash = await polkadot.query.system.blockHash(currentBlockNumber);final genesisHash = await polkadot.query.system.blockHash(0);final nonce = await polkadot.rpc.system.accountNextIndex(wallet.address);
// Make the encoded callfinal multiAddress = $MultiAddress().id(wallet.publicKey.bytes);final transferCall = polkadot.tx.balances.transferKeepAlive(dest: multiAddress, value: BigInt.one).encode();
// Make the payloadfinal payload = SigningPayload( method: transferCall, specVersion: runtimeVersion.specVersion, transactionVersion: runtimeVersion.transactionVersion, genesisHash: encodeHex(genesisHash), blockHash: encodeHex(currentBlockHash), blockNumber: currentBlockNumber, eraPeriod: 64, nonce: nonce, tip: 0,).encode(polkadot.registry);
// Sign the payload and build the final extrinsicfinal signature = wallet.sign(payload);final extrinsic = ExtrinsicPayload( signer: wallet.bytes(), method: transferCall, signature: signature, eraPeriod: 64, blockNumber: currentBlockNumber, nonce: nonce, tip: 0,).encode(polkadot.registry, SignatureType.sr25519);
// Send the extrinsic to the blockchainfinal author = AuthorApi(provider);await author.submitAndWatchExtrinsic(extrinsic, (data) { print(data);});Where to Go Next
Section titled “Where to Go Next”To dive deeper into Polkadart, refer to the official Polkadart documentation, where you can find comprehensive guides for common use cases and advanced usage.