我正在flutter上开发一个dapp(去中心化应用程序),这是一个基本的应用程序,具有基本的智能合约和函数,我创建了一个getbalance方法和submit方法,分别在交易时获取余额、取款和存款函数。callcontract部分我添加了fetchidfromnetwork,我收到了此错误
错误:
E/flutter (30816): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Invalid argument(s): You can't specify loadChainIdFromNetwork and specify a custom chain id!
E/flutter (30816): #0 _fillMissingData (package:web3dart/src/core/transaction_signer.dart:20:5)
E/flutter (30816): #1 Web3Client.signTransaction (package:web3dart/src/core/client.dart:316:32)
E/flutter (30816): #2 Web3Client.sendTransaction (package:web3dart/src/core/client.dart:285:24)
E/flutter (30816): #3 _MyHomePageState.Submit (package:mad_froggys/main.dart:133:36)
E/flutter (30816): <asynchronous suspension>
E/flutter (30816): #4 _MyHomePageState.Sendcoin (package:mad_froggys/main.dart:142:20)
E/flutter (30816): <asynchronous suspension>
E/flutter (30816):
这是我代码:main.dart
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart';
import 'package:mad_froggys/slider.dart';
import 'package:velocity_x/velocity_x.dart';
import 'package:web3dart/web3dart.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Mad froggys'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late Client httpClient;
late Web3Client ethClient;
bool data = false;
int myAmount = 1;
final myAdress = "0x0FD0F0ED218D5D91C9fB702E53c2FA1C65bCF805";
var myData;
@override
void initState() {
httpClient = Client();
ethClient = Web3Client("https://rinkeby.infura.io/v3/ff6f20460b7543dea923f0b9f987b2ac", httpClient);
super.initState();
getBalance(myAdress);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Vx.gray300,
body:ZStack([
VxBox()
.green600
.size(context.screenWidth,context.percentHeight*30)
.make(),
VStack([
(context.percentHeight*10).heightBox,
"\$Madfroggys".text.xl4.white.center.bold.makeCentered().py16(),
(context.percentHeight*5).heightBox,
VxBox(child: VStack([
"Balance".text.gray500.bold.xl2.makeCentered(),
10.heightBox,
data?"\$$myData".text.bold.green600.xl4.makeCentered().shimmer():CircularProgressIndicator().centered(),
]).py16())
.white
.size(context.screenWidth,context.percentHeight*15)
.rounded
.shadowXl
.make()
.p16(),
30.heightBox,
SliderWidget(min: 0,max: 100,finalvalue: (value){
myAmount = (value*100).round();
print(myAmount);
},).centered(),
HStack([
FlatButton.icon(
onPressed:()=>getBalance(myAdress), icon:Icon(Icons.refresh,color: Colors.white,),
color: Colors.cyan,
shape: Vx.roundedSm,
label: "refresh".text.white.make()),
FlatButton.icon(
onPressed:()=>Sendcoin(), icon:Icon(Icons.refresh,color: Colors.white,),
color: Colors.cyan,
shape: Vx.roundedSm,
label: "deposit".text.white.make()),
FlatButton.icon(
onPressed:()=>Withdrawcoin(), icon:Icon(Icons.refresh,color: Colors.white,),
color: Colors.cyan,
shape: Vx.roundedSm,
label: "withdraw".text.white.make())
],alignment: MainAxisAlignment.spaceAround,axisSize: MainAxisSize.max,
).p16()
]),
])
);
}
//for reading the balance
Future<void> getBalance(String targetAdress) async {
// EthereumAddress adress = EthereumAddress.fromHex(targetAdress);
List<dynamic> result = await query("getBalance",[]);//will give the function and argument to query
myData = result[0];
data = true;
setState(() {
});
}
// submit metheod
Future<String>Submit(String functionName,List<dynamic> args) async{
EthPrivateKey credentials = EthPrivateKey.fromHex("84a6fa216233a59b01c4a35acdf1501f0797130acf23f81420432d67d8805f59");
DeployedContract contract = await loadContract();
final ethFunction = contract.function(functionName);
**final result = await ethClient.sendTransaction(credentials,
Transaction.callContract(contract: contract, function:ethFunction,
parameters:args),fetchChainIdFromNetworkId: true);**
return result;
}
// writing and sending balance
Future<String>Sendcoin() async{
var bigAmount = BigInt.from(myAmount);
var response = await Submit("withdrawBalance",[bigAmount]);
print("sended");
return response;
}
//writing and withdraw balance
Future<String>Withdrawcoin() async{
var bigAmount = BigInt.from(myAmount);
var response = await Submit("depositBalance",[bigAmount]);
print("withdrawn");
return response;
}
Future<DeployedContract>loadContract()async{
String abi = await rootBundle.loadString("assets/abi.json");
String contractAdress = "0xBed4D1f72eFB636CEc6Dea03f3522D0db51d556d";
final contract = DeployedContract(ContractAbi.fromJson(abi,"MADfroggys"),EthereumAddress.fromHex(contractAdress));
return contract;
}
Future<List<dynamic>>query(String functionName,List<dynamic>args) async{
final contract = await loadContract(); // we have to load the contract
final ethFunction = contract.function(functionName); // function name is given as argument
final result = await ethClient.call(contract:contract, function:ethFunction, params:args);
return result;
}
}
我不知道该怎么办
1条答案
按热度按时间cbjzeqam1#
你好Abhiram你应该把
chainId
设置为null
当你把fetchChainIdFromNetworkId
设置为true
的时候,这样你的Submit函数就像这样: