搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.2

flutter_web3_provider

Flutter web web3 provider

近 30 天下載
80
喜歡數
20
Pub 分數
25

安裝套件

flutter pub add flutter_web3_provider

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 套件,可實現與 web3 技術的整合,適用於網路應用程式,讓使用者能直接從其 Flutter 網路應用與以太坊為基礎的區塊鏈互動。它提供提供者模式的實作,用來管理區塊鏈連接、帳戶狀態與交易處理。專為網路平台設計,簡化對 MetaMask 等錢包的存取,並支援標準的 web3 方法,例如簽署交易與讀取智慧合約資料。此套件輕量且設計為與 Flutter 的狀態管理生態系統無縫運作。

適用情境

  • 在網頁應用中連接以太坊錢包
  • 從智能合約讀取數據
  • 在鏈上簽署交易
  • 管理用戶錢包帳戶
  • 啟用去中心化應用(dApp)功能

主要特色

  • 僅限 Web 的 web3 結合
  • 基於 Provider 的狀態管理
  • 支援 MetaMask 與類似錢包
  • 用於常見 web3 操作的簡單 API
  • 內建的連接與斷開處理

適合對象

  • Flutter 開發者建構 dApps
  • 基於網絡的區塊鏈應用
  • 需要錢包連接的項目
  • 針對以太坊相容鏈的使用者
  • 使用 Flutter 反應式 UI 框架的開發人員

使用建議

  • 僅限網頁平台
  • 需要瀏覽器錢包支援
  • 依賴外部錢包擴展
  • 不適用於行動或桌面應用程式
  • 可能需要額外的錯誤處理來應對網路問題

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

flutter_web3_provider

Flutter wrapper for using web3 providers, ie: accessing window.ethereum.

NOTE: This is for web only!

Getting Started

For full example, see: https://github.com/gochain/flutter_web3_provider/blob/main/example/lib/main.dart

Add import import 'package:flutter_web3_provider/ethereum.dart';

Then you can access it just be using the ethereum variable.

if(ethereum != null){
    // then an ethereum provider was injected
    print(ethereum.selectedAddress);
}

Ask user to connect their wallet:

RaisedButton(
    child: Text("Connect Wallet"),
    onPressed: () async {
        var accounts = await promiseToFuture(
            ethereum.request(RequestParams(method: 'eth_requestAccounts')));
        print(accounts);
        String se = ethereum.selectedAddress;
        print("selectedAddress: $se");
        setState(() {
            selectedAddress = se;
        });
    },
)
Using ethers.js

Add ethers.js to web/index.html.

  <script type="module">
    import { ethers } from "https://cdnjs.cloudflare.com/ajax/libs/ethers/6.7.0/ethers.min.js";
    window.ethers = ethers
    // Your code here...
  </script>

Import package:

import 'package:flutter_web3_provider/ethers.dart';

Then create an ethers provider:

// For a read-only provider:
var provider = JsonRpcProvider("https://rpc.gochain.io");
// For a read-write provider (ie: metamask, trust wallet, etc)
var web3 = BrowserProvider(ethereum);

Then you can do things like check balance and submit transactions, etc:

var abalanceF = promiseToFuture(web3.getBalance(ethereum.selectedAddress));

Future tx = promiseToFuture(web3.Signer().sendTransaction(TxParams(
      to: to,
      value: "0x" +
          BigInt.parse(toBase(amount, 18).toString()).toRadixString(16))));

Or use a contract:

const erc20Abi = [
    // Some details about the token
    "function name() view returns (string)",
    "function symbol() view returns (string)",

    // Get the account balance
    "function balanceOf(address) view returns (uint)",

    // Send some of your tokens to someone else
    "function transfer(address to, uint amount)",

    // An event triggered whenever anyone transfers to someone else
    "event Transfer(address indexed from, address indexed to, uint amount)"
];
var contract = Contract(contractAddress, erc20Abi, web3);
// call balanceOf function
var usdcBalanceF = promiseToFuture(
          callMethod(contract, "balanceOf", [ethereum.selectedAddress]));
          
// to make a write transaction, first get the signer (this will use metamask/wallet)
contract = contract.connect(await promiseToFuture(web3.getSigner())); // uses the connected wallet as signer
// then call the function:
var res =
    await promiseToFuture(callMethod(contract, "transfer", [
    '0x39C5190c09ec04cF09C782bA4311C469473Ffe83',
    "0x" + amount.toString()).toRadixString(16)
    ]));

If you need to additional overrides like set gas price or passing the value param, you can add one extra param in the array with those fields, eg:

var res = await promiseToFuture(callMethod(
    contract, "transfer", ['0x39C5190c09ec04cF09C782bA4311C469473Ffe83', "0x" + amount.toString()).toRadixString(16), 
    TxParams(value: "1000000000000000000")]));

NOTES:

  • If you're using the human readable ABI's (ethers.js feature) like above, use uint, not uint256 even if the real abi is a uint256.
  • There are some common functions on the Contract, but you can also call any method using callMethod like above.

Developers

To publish:

make publish

所屬分類

依賴

依賴清單 1 項

  • js^0.7.1

原始碼儲存庫

使用此套件的專案

暫無相關資料。