首页 > 解决方案 > Flutter - SocketException:连接失败(操作系统错误:网络无法访问,errno = 101)

问题描述

连接到以太坊节点时出错

E/flutter(23790):[错误:flutter/lib/ui/ui_dart_state.cc(148)]未处理的异常:SocketException:连接失败(操作系统错误:网络无法访问,errno = 101)

---------------------------------- web3连接代码如下------------ ----------------------------------

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

标签: dartflutterweb3

解决方案


我仍然无法弄清楚为什么这个解决方案有效,但我遇到了同样的问题,我试图localhost用我的服务器的 IP 地址替换(例如192.168.1.33)。有效!。

为您的应用尝试以下代码:

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

推荐阅读