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());
}
7
  • 1
    Do you run your app on a simulator? Commented Jul 12, 2019 at 14:08
  • yes I am using simulator Commented Jul 13, 2019 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. Commented Jul 13, 2019 at 12:06
  • I am running ethereum node on my local system, I used that RPC URL (localhost:8545) in nodejs application and it's working fine Commented Jul 16, 2019 at 11:38
  • Sorry, I didn't get you, do you mean this URL is working fine on your local machine or the emulator? Commented Jul 16, 2019 at 17:18

1 Answer 1

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());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.