Flutter-SocketException:连接失败(操作系统错误:网络无法访问,errno = 101)

时间:2019-03-28 14:29:15

标签: dart flutter web3

我是Flutter应用程序开发的新手,我不知道这里发生了什么
请帮助我解决此错误

  

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 = 'http://62.20.4.514:8547';
class HomeScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomeScreenState();
  }
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    main();
    super.initState();
  }
  main(){
  var httpClient = new Client();
  var ethClient = new Web3Client(apiUrls, httpClient);
  print(ethClient.getBlockNumber());
}

让我知道我是否在编码方面犯了一些错误,或者帮助我如何在波动中使用web3连接

1 个答案:

答案 0 :(得分:0)

我仍然不知道为什么该解决方案有效,但是我遇到了同样的问题,我尝试用服务器的IP地址(例如localhost)替换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());
}