Skip to content

Connecting to a Network

After installing Polkadart and generating types, you’re ready to connect to a network and start querying the blockchain.

Create a new file bin/demo.dart and add the following code:

demo.dart
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());
}

Execute your application with:

Terminal window
dart run bin/demo.dart

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!

final provider = Provider.fromUri(Uri.parse('wss://rpc.polkadot.io'));

You can customize your connection with additional options:

final provider = Provider.fromUri(
Uri.parse('wss://rpc.polkadot.io'),
// Add connection options here if needed
);