DEV Community

abhiram-ms
abhiram-ms

Posted on

Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast error when trying to call function with no int

Hello I was calling a startelection function from my flutter app on a button click and I got this error the startelection function connects my flutter app with my solidity smart contract and starts the election through infura I don't know why I am getting this error it was running fine before

error :

E/flutter (31094): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast
E/flutter (31094): #0 JsonRPC.call (package:web3dart/json_rpc.dart:53:27)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #1 Web3Client._makeRPCCall (package:web3dart/src/core/client.dart:54:20)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #2 Web3Client.getGasPrice (package:web3dart/src/core/client.dart:175:18)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #3 _fillMissingData (package:web3dart/src/core/transaction_signer.dart:36:16)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #4 Web3Client.signTransaction (package:web3dart/src/core/client.dart:316:26)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #5 Web3Client.sendTransaction (package:web3dart/src/core/client.dart:285:18)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #6 startElection (package:election/services/functions.dart:29:18)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094): #7 _HomeState.build.<anonymous closure> (package:election/pages/home.dart:53:25)
E/flutter (31094): <asynchronous suspension>
E/flutter (31094):

home.dart
`
import 'package:election/utils/Constants.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';

import '../services/functions.dart';
import 'Electioninfo.dart';

class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);

@override
State createState() => _HomeState();
}

class _HomeState extends State {
late Client? httpClient;
late Web3Client? ethclient;
TextEditingController controller = TextEditingController();

@override
void initState() {
httpClient = Client();
ethclient = Web3Client(infura_url, httpClient!);
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Start Election'),
),
body: Container(
padding: const EdgeInsets.all(14),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: controller,
decoration: const InputDecoration(
filled: true, hintText: 'Enter election name'),
),
const SizedBox(
height: 10,
),
Container(
width: double.infinity,
height: 45,
child: ElevatedButton(
onPressed: () async {
if (controller.text.isNotEmpty) {
await startElection(controller.text, ethclient!);
if(!mounted)return;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ElectionInfo(
ethClient: ethclient!,
electionName: controller.text,
)));
}
},
child: const Text('Start Election')))
],
),
),
);
}
}`

functions.dart:

`Future startElection(String name,Web3Client ethclient) async{

var response = await callFunction('startElection', [name], ethclient, owner_private_key);
print("election started successfully");

return response;
}

Future callFunction(String funcname,Listargs,Web3Client ethClient,String privateKey) async{

EthPrivateKey credentials = EthPrivateKey.fromHex(privateKey);
DeployedContract contract = await loadcontract();

final ethfunction = contract.function(funcname);
final result = ethClient.sendTransaction(
credentials,Transaction.callContract(contract: contract, function: ethfunction, parameters: args),
chainId: null,fetchChainIdFromNetworkId: true);

return result;
}`

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
ayevbeosa profile image
Ayevbeosa Iyamu

Most likely the JSON response isn't what the JSON parser was expecting.

Sentry growth stunted Image

If you are wasting time trying to track down the cause of a crash, it’s time for a better solution. Get your crash rates to zero (or close to zero as possible) with less time and effort.

Try Sentry for more visibility into crashes, better workflow tools, and customizable alerts and reporting.

Switch Tools

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay