Flutter: 从 Window 对象获取自定义属性

huangapple go评论123阅读模式
英文:

Flutter: Get custom properties from Window Object

问题

在文档中提到,您可以通过检查window.ethereum != undefined(在JS中)来确定Metamask是否已安装。

显然,在Flutter的Window对象中没有这个属性。是否有一个列出所有未知属性或类似的变量?

谢谢!

英文:

I am working on a Web3 Flutter website (just to learn flutter).

I would like to programmatically know if there is an extension installed or not (Metamask in this case).
In the documentation they say that I can check if Metamask is installed by checking if window.ethereum != undefined (in JS).

Obviously, I don't have this property in the Window object of flutter..
Is there a variable that list all unknown properties or something like that?

Thanks!

答案1

得分: 1

你可以使用这个插件 https://pub.dev/packages/flutter_web3

有一个属性可以用作

  • Ethereum.isSupported 等同于 ethereum != null
    如果 (ethereum != null) {
    尝试 {
    // 提示用户连接到提供者,即确认连接模态框
    final accs = await ethereum!
    .requestAccount(); // 获取节点中的所有帐户
    accs; // [foo, bar]
    } on EthereumUserRejected {
    print('用户拒绝了模态框');
    }
    }
英文:

you can use this plugin https://pub.dev/packages/flutter_web3

there is a property that you can use as

    / `Ethereum.isSupported` is the same as `ethereum != null`
if (ethereum != null) {
  try {
    // Prompt user to connect to the provider, i.e. confirm the connection modal
    final accs = await ethereum!
        .requestAccount(); // Get all accounts in node disposal
    accs; // [foo,bar]
  } on EthereumUserRejected {
    print('User rejected the modal');
  }
}

huangapple
  • 本文由 发表于 2023年3月31日 20:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定