Connecting to a Network
After installing Polkadart and generating types, you’re ready to connect to a network and start querying the blockchain.
Setting Up Your Connection
Section titled “Setting Up Your Connection”Using Provider with Generated APIs
Section titled “Using Provider with Generated APIs”Create a new file bin/demo.dart
and add the following code:
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);
final query = await polkadot.rpc.state.getRuntimeVersion(); print(query.toJson());}
Running Your Application
Section titled “Running Your Application”Execute your application with:
dart run bin/demo.dart
Expected Output
Section titled “Expected Output”You should see the runtime version of the Polkadot chain:
{ specName: polkadot, implName: parity-polkadot, authoringVersion: 0, specVersion: 1003003, implVersion: 0, apis: [...], transactionVersion: 26, stateVersion: 1}
🎉 Success! You’ve successfully connected to Polkadot and made your first query to the chain!
Connecting to Different Networks
Section titled “Connecting to Different Networks”final provider = Provider.fromUri(Uri.parse('wss://rpc.polkadot.io'));
final provider = Provider.fromUri(Uri.parse('wss://kusama-rpc.polkadot.io'));
final provider = Provider.fromUri(Uri.parse('wss://westend-rpc.polkadot.io'));
final provider = Provider.fromUri(Uri.parse('ws://127.0.0.1:9944'));
Connection Options
Section titled “Connection Options”You can customize your connection with additional options:
final provider = Provider.fromUri( Uri.parse('wss://rpc.polkadot.io'), // Add connection options here if needed);