Dart and Flutter developers can use Web3Dart for Ethereum development, enabling cross-platform mobile and web applications.
The primary Ethereum library for Dart and Flutter ecosystems, providing comprehensive blockchain interaction capabilities.
import 'package:web3dart/web3dart.dart';
import 'package:http/http.dart';
class EthereumService {
final Web3Client client;
EthereumService(String rpcUrl) : client = Web3Client(rpcUrl, Client());
Future<EtherAmount> getBalance(String address) async {
final addr = EthereumAddress.fromHex(address);
return await client.getBalance(addr);
}
Future<String> sendTransaction({
required Credentials credentials,
required String to,
required BigInt amount,
}) async {
final transaction = await client.sendTransaction(
credentials,
Transaction(
to: EthereumAddress.fromHex(to),
value: EtherAmount.fromBigInt(EtherUnit.wei, amount),
),
);
return transaction;
}
}
Important Note: Always specify a gas limit when creating transactions to prevent unexpected transaction failures. The Web3Dart library will attempt to estimate gas but explicit values are recommended for production applications.
Example of a Flutter widget using Web3Dart to display wallet balance:
import 'package:flutter/material.dart';
import 'package:web3dart/web3dart.dart';
class WalletWidget extends StatefulWidget {
_WalletWidgetState createState() => _WalletWidgetState();
}
class _WalletWidgetState extends State<WalletWidget> {
final ethereum = EthereumService('YOUR_ETHEREUM_NODE_URL');
EtherAmount? balance;
Future<void> updateBalance() async {
final newBalance = await ethereum.getBalance('YOUR_ADDRESS');
setState(() {
balance = newBalance;
});
}
Widget build(BuildContext context) {
return Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
Text('Balance: ${balance?.getValueInUnit(EtherUnit.ether)} ETH'),
ElevatedButton(
onPressed: updateBalance,
child: Text('Refresh Balance'),
),
],
),
),
);
}
}
Performance Tip: For production Flutter apps, consider implementing caching mechanisms and background refresh patterns to avoid excessive RPC calls that might impact app responsiveness.
Working with smart contracts in Dart with complete type safety:
class SmartContractService {
final Web3Client client;
final DeployedContract contract;
SmartContractService(String rpcUrl, String contractAddress, String abi)
: client = Web3Client(rpcUrl, Client()),
contract = DeployedContract(
ContractAbi.fromJson(abi, 'YourContract'),
EthereumAddress.fromHex(contractAddress),
);
Future<List<dynamic>> callFunction(
String functionName,
List<dynamic> params,
) async {
final function = contract.function(functionName);
final result = await client.call(
contract: contract,
function: function,
params: params,
);
return result;
}
}
Security Note: When handling private keys in Flutter applications, use secure storage solutions like
flutter_secure_storage
to protect sensitive credentials. Never hardcode private keys in your application code.
Proper error handling in Dart for robust Ethereum applications:
class EthereumException implements Exception {
final String message;
final dynamic originalError;
EthereumException(this.message, [this.originalError]);
String toString() => 'EthereumException: $message';
}
extension EtherAmountExtension on EtherAmount {
String formatEther([int decimals = 4]) {
return getValueInUnit(EtherUnit.ether).toStringAsFixed(decimals);
}
}