Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast error when trying to call function with no int
Tags: flutter, null, dart, vscode
Published: 2022-10-16 11:12:59

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<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  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<String> startElection(String name,Web3Client ethclient) async{

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

  return response;
}


Future<String> callFunction(String funcname,List<dynamic>args,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;
}`