How to connect web3 to ethereum network using flutter?

2.2k Views Asked by At

I am getting error while connecting with ethereum node.

  • this line ==> print(client.getBlockNumber()); returns error

E/flutter (22015): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: SocketException: Failed host lookup: 'localhost:8545' (OS Error: No address associated with hostname, errno = 7)

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
import 'dart:async';
const String url =
    'https://mainnet.infura.io'; // i am also running geth on local http://localhost:8545

class HomeScreen extends StatefulWidget {
    @override
    State<StatefulWidget> createState() {
        return _HomeScreenState();
    }
}

class _HomeScreenState extends State<HomeScreen> {
    @override
    void initState() {
        main();
        super.initState();
    }
    main() {
        final httpClient =new Client();
        final client =new  Web3Client(url, httpClient);
        print(client.getBlockNumber());
    }
}
1

There are 1 best solutions below

0
Abhik Banerjee On

Port 8545 on Localhost is generally used when you are doing some development on Local Ethereum Blockchain Network. Most probable cause in this case is that you are not running a Client or Wallet service like Ganache from where the injection is going to take place. Ganache creates a local wallet and gives you 10 development Eth Address to work with. You can use that with your App. Your RPC port here is 8545.