简体   繁体   English

如何使用颤振将 web3 连接到以太坊网络?

[英]How to connect web3 to ethereum network using flutter?

I am getting error while connecting with ethereum node.连接以太坊节点时出现错误。

  • this line ==> print(client.getBlockNumber());这一行 ==> 打印(client.getBlockNumber()); returns error返回错误

E/flutter (22015): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: SocketException: Failed host lookup: 'localhost:8545' (OS Error: No address associated with hostname, errno = 7) E/flutter (22015): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] 未处理的异常:SocketException:主机查找失败:'localhost:8545'(操作系统错误:没有与主机名关联的地址,errno = 7 )

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
import 'dart:async';
const String url =
    'https://mainnet.infura.io'; // i am also running geth on local http://localhost:8545

class HomeScreen extends StatefulWidget {
    @override
    State<StatefulWidget> createState() {
        return _HomeScreenState();
    }
}

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

Port 8545 on Localhost is generally used when you are doing some development on Local Ethereum Blockchain Network.当您在本地以太坊区块链网络上进行一些开发时,通常会使用 Localhost 上的端口 8545。 Most probable cause in this case is that you are not running a Client or Wallet service like Ganache from where the injection is going to take place.在这种情况下,最可能的原因是您没有运行像 Ganache 这样的客户端或钱包服务,从那里进行注入。 Ganache creates a local wallet and gives you 10 development Eth Address to work with. Ganache 创建了一个本地钱包,并为您提供 10 个开发 Eth 地址以供使用。 You can use that with your App.您可以将它与您的应用程序一起使用。 Your RPC port here is 8545.你这里的 RPC 端口是 8545。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM