发送交易时出现错误:typeerror:无法读取flutter中区块链应用程序中未定义的属性(读取“提供者”)


  1  import 'package:flutter_web3/flutter_web3.dart';
  2  import 'package:web3dart/web3dart.dart' as web;
  3  import 'package:web3dart/credentials.dart';
  4  
  5  String currentAddress = '';
  6  bool get isEnabled = ethereum != null;
  7  bool get isConnected =isEnabled && currentAddress.isNotEmpty;
  8  
  9  Future<bool> sendTransaction({required EthereumAddress to}) async {
 10      if (isEnabled) {
 11          final accs = await ethereum!.requestAccount();
 12          if (accs.isNotEmpty)
 13              currentAddress = accs.first; //assign current address to first address
 14          if (currentAddress != '') {
 15              print(currentAddress);
 16          }
 17      }
 18      if (!isConnected) {
 19          return false;
 20      }
 21      try {
 22          final eth = Ethereum.ethereum;
 23          final transaction = web.Transaction(
 24              from: EthereumAddress.fromHex(currentAddress),
 25              to: to,
 26              value: web.EtherAmount.inWei(BigInt.parse("8000")),
 27          );
 28          if (ethereum != null) {
 29              print("Etherem is not null");
 30          }
 31  
 32          final web3provider = Web3Provider(ethereum!);
 33          final signer = provider?.getSigner();
 34          final tx = await signer?.sendTransaction(transaction as TransactionRequest);
 35  
 36          if (tx != null) {
 37              print(tx.hashCode);
 38              return true;
 39          }
 40  
 41          print("Transaction is not performed");
 42          return false;
 43      } catch (e) {
 44          print("Errror sending transaction : $e");
 45          return false;
 46      }
 47  }

我尝试过的:

在 chrome(元掩码扩展)中运行此代码时,元掩码将弹出以连接帐户,但之后它会给出 TypeError: Cannot read properties of undefined (reading ‘providers’),我相信错误存在,最终 web3provider = Web3Provider(ethereum! ); 但我不知道为什么以及如何解决。

解决方案1

消息“无法读取未定义的属性”是 javascript 语言,用于尝试使用不存在的对象或在其他语言中为 null 的对象。

我没有看到您发布的代码中的任何地方调用“providers”属性或方法,因此这意味着您尝试使用的库可能会引发错误。 唯一可以帮助您的人是编写该库的人。

コメント

タイトルとURLをコピーしました