1

Getting error while connecting to ethereum node

E/flutter (23790): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: SocketException: Connection failed (OS Error: Network is unreachable, errno = 101)

---------------------------------- web3 connection code is below -----------------------------------------------

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
import 'dart:async';

const String rpcUrls = 'https://node1.bitcoiin.com';
class HomeScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomeScreenState();
  }
}
class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    main();
    super.initState();
  }
  main(){
  var ethClient = new Web3Client(apiUrls, new Client());
  print(ethClient.getBlockNumber());
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Ishwar Chandra Tiwari
  • 5,099
  • 6
  • 22
  • 41
  • 1
    Do you run your app on a simulator? – Ahmed Kamal Jul 12 '19 at 14:08
  • yes I am using simulator – Ishwar Chandra Tiwari Jul 13 '19 at 11:11
  • For some reason, your emulator can't look up for your server's IP (it's a network issue not flutter issue), try to open the same URL from the browser to make sure. – Ahmed Kamal Jul 13 '19 at 12:06
  • I am running ethereum node on my local system, I used that RPC URL (http://localhost:8545) in nodejs application and it's working fine – Ishwar Chandra Tiwari Jul 16 '19 at 11:38
  • Sorry, I didn't get you, do you mean this URL is working fine on your local machine or the emulator? – Ahmed Kamal Jul 16 '19 at 17:18
  • Possible duplicate of [How to point to localhost:8000 with the Dart http package in Flutter?](https://stackoverflow.com/questions/47372568/how-to-point-to-localhost8000-with-the-dart-http-package-in-flutter) – Salvatore Aug 25 '19 at 10:43
  • @IshwarChandraTiwari i already defined my local ip adress 10.xxx.xx.xxx still getting socket exception error app working fine on emulator of my flutter project but on Real devices its giving error all time of Socket exception. – s.j Apr 09 '21 at 11:13

1 Answers1

1

I still can't figure out why this solution works, but I had the same problem and I tried to replace localhost with the IP address to my server (e.g. 192.168.1.33). It worked!.

Try this code for your app:

main(){
  var httpClient = new Client();
  // You tried the code below and it didn't work
  // var ethClient = new Web3Client('http://localhost:8545', httpClient);

  // Try this code instead. (Replace "192.168.1.33" with the IP of your server)
  var ethClient = new Web3Client('http://192.168.1.33:8545', httpClient);

  print(ethClient.getBlockNumber());
}
Salvatore
  • 499
  • 10
  • 16