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 }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)