Bắt đầu với TheRPC
Tham khảo API
Ethereum API
Core API
Hướng dẫn

Dart & Flutter Tools for Ethereum

Dart and Flutter developers can use Web3Dart for Ethereum development, enabling cross-platform mobile and web applications.

Chú ý!
Xin chào! Lưu ý thân thiện: trang này nhằm cung cấp cho bạn tổng quan chi tiết về việc làm việc với Dart/Flutter các yêu cầu. Đối với ví dụ mã thực tế, hãy xem API Methods documentation nơi bạn sẽ tìm thấy các ví dụ sẵn sàng sử dụng trong tất cả các ngôn ngữ được hỗ trợ!

# Web3Dart Library

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;
  }
}
  • GitHub: web3dart
  • Pub.dev: web3dart package
  • Features:
    • Flutter support for cross-platform mobile dApps
    • Seamless integration with Flutter widgets
    • Complete contract interaction capabilities
    • Secure transaction signing and verification
    • HD wallet integration with BIP-39/44 support
    • ENS resolution and integration
    • ERC-20 and ERC-721 token standards support
    • Gas estimation and optimization

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.

# Flutter Integration Examples

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.

# Smart Contract Integration

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.

# Error Handling and Utilities

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);
  }
}

See also

Giúp chúng tôi trở nên tốt hơn!
Chia sẻ trang này và giúp chúng tôi tạo ra sản phẩm tốt hơn cho bạn.