Giter Site home page Giter Site logo

Comments (14)

liuyuejiang avatar liuyuejiang commented on September 3, 2024 2

I found the problem. In ganache, in the setting, the server's host name is 127.0.0.0 initially, I switch to 0.0.0.0 and it works well!!! Probably, for geth node, the problem is rpcdomain property.
Thank you so much! Finally, I spend a whole night to solve this problem.

from web3dart.

simolus3 avatar simolus3 commented on September 3, 2024 1

Right, but the Android emulator will also setup a virtual network, so you can't reach your host through 0.0.0.0. You can use 10.0.2.2 though, as explained here.

from web3dart.

simolus3 avatar simolus3 commented on September 3, 2024 1

No, it wouldn't work on a real device. In that case you'd have to lookup the IP address of the host. Inside your local network it probably starts with 192.168, so you could use that address for devices inside your local network.

from web3dart.

liuyuejiang avatar liuyuejiang commented on September 3, 2024
var privatekey = '0x6166244d506a4c1635e0a0723cda3780fa80462a869682xxxxxxxxxx';
var apiUrl = "http://10.0.0.47:8545"; //Replace with your API

var httpClient = new Client();
var ethClient = new Web3Client(apiUrl, httpClient);
// String data = await DefaultAssetBundle.of(context).loadString(
//     "assets/web3/8acd17e6f4fef86fdf75078d179ec2612fb354ae.json");
// final jsonResult = json.encode(data);

// Wallet wallet = Wallet.fromJson(data, "aa");
var credentials = Credentials.fromPrivateKeyHex(privatekey);

// Credentials unlocked = wallet.credentials;
EtherAmount balance = await ethClient.getBalance(credentials.address);
print(balance.getValueInUnit(EtherUnit.ether));

I tried to use both methods. It didn't work.

from web3dart.

simolus3 avatar simolus3 commented on September 3, 2024

If you try something like

var httpClient = new Client();
await httpClient.get('$apiUrl/');

before starting your ethClient. Does that also throw the exception?
If so, you might be missing the internet permission in your Flutter app. Maybe this helps?

from web3dart.

liuyuejiang avatar liuyuejiang commented on September 3, 2024

If you try something like

var httpClient = new Client();
await httpClient.get('$apiUrl/');

before starting your ethClient. Does that also throw the exception?
If so, you might be missing the internet permission in your Flutter app. Maybe this helps?

It doesn't help when I add await httpClient.get('$apiUrl/');, the same exception.
This time I deployed my smart contract on ganache, when I tried to connect, it shows the same exception.
Could it be a problem with MacOS? Do I need to switch to Linux to deploy my smart contract?

from web3dart.

simolus3 avatar simolus3 commented on September 3, 2024

I don't think it's an issue with your operating system. Rather, the error indicates that your Flutter app is unable to reach the network.

Are you on Android? If so, try adding <uses-permission android:name="android.permission.INTERNET" /> to the AndroidManifest.xml in the android/app folder of your Flutter project.

from web3dart.

liuyuejiang avatar liuyuejiang commented on September 3, 2024

I have already added the permission long long ago, the app can access the Internet well.
Do I need to provide other environment information?

from web3dart.

simolus3 avatar simolus3 commented on September 3, 2024

Hm, that's really weird.
But just to be sure, if you remove the web3dart related code of that snippet entirely and just try to make a request with the http package like this:

import "package:http/http.dart" as http;

await http.get("http://10.0.0.47:8545");

You do get the same problem, right?

I don't really get how your app can access the internet while still getting connection errors in web3dart. This library delegates all the work to the Client(), which is part of the http package. If you're making http requests elsewhere in your app, you're probably already using that package. It's weird how that works while there are errors in web3dart.

I'm not really familiar with MacOS unfortunately. I might be that there is a firewall blocking that port, even from the local network?

from web3dart.

liuyuejiang avatar liuyuejiang commented on September 3, 2024

Hm, that's really weird.
But just to be sure, if you remove the web3dart related code of that snippet entirely and just try to make a request with the http package like this:

import "package:http/http.dart" as http;

await http.get("http://10.0.0.47:8545");

You do get the same problem, right?

I don't really get how your app can access the internet while still getting connection errors in web3dart. This library delegates all the work to the Client(), which is part of the http package. If you're making http requests elsewhere in your app, you're probably already using that package. It's weird how that works while there are errors in web3dart.

I'm not really familiar with MacOS unfortunately. I might be that there is a firewall blocking that port, even from the local network?

I do have these permissions, like:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.hardware.location.gps" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

So, that's why confusing. I will try to use http tomorrow. Thank you, I will let you know.

from web3dart.

PeterTF656 avatar PeterTF656 commented on September 3, 2024

@simolus3 @liuyuejiang I am running into a very similar issue... In my case, my app runs fine on an ios simulator, but on an actual android device/emulator, it gives out E/flutter ( 6625): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = 0.0.0.0, port = 51300, and I have added in the permission for android to access internet.

When I opened the dart dev tool, in the Network tab, the ws and http methods both showed pending.

Any help is appreciated...

from web3dart.

simolus3 avatar simolus3 commented on September 3, 2024

Why would you connect to 0.0.0.0? Surely there's no Ethereum node running on the actual emulator?

from web3dart.

PeterTF656 avatar PeterTF656 commented on September 3, 2024

Why would you connect to 0.0.0.0? Surely there's no Ethereum node running on the actual emulator?

Thank you sir for your quick reply. I used Ganache and I changed the server hostname to 0.0.0.0 - all interfaces

from web3dart.

PeterTF656 avatar PeterTF656 commented on September 3, 2024

Right, but the Android emulator will also setup a virtual network, so you can't reach your host through 0.0.0.0. You can use 10.0.2.2 though, as explained here.

Yes sir, It did work. Thank you! One more question, how about a real android device? Will it work with 10.0.2.2 Ganache?

from web3dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.