Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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  }


What I have tried:

while running this code in chrome(metamask extension), metamask will popup for connecting for account but after that it gives TypeError: Cannot read properties of undefined (reading 'providers'), I believe error is in, final web3provider = Web3Provider(ethereum!); but I don't know why and how to resolve.
Posted
Updated 7-Feb-24 22:36pm
v2
Comments
Richard MacCutchan 8-Feb-24 3:48am    
The error message is telling you that you have passed a null reference (uninitialised object) to some function or method. You will need to do some debugging to find out where the problem is caused from.

1 solution

The message "Cannot read properties of undefined" is javascript-ese for trying to use an object that doesn't exist, or is null in other lanugages.

I don't see a "providers" property or method being called anywhere in the code you posted, so that means the error is probably being thrown by the library you're trying to use. The only people that can help you with that are the people that wrote the library.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900