Flutter - SocketException: Connection failed (OS Error: Network is unreachable, errno = 101)

4.4k Views Asked by At

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());
}
1

There are 1 best solutions below

0
On

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());
}