搜索插件、代码仓与 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

源码仓库

使用此插件的项目

暂无相关数据。