Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled Exception: SocketException: Connection refused / Connection timed out, host: #135

Open
TahaKh99 opened this issue Jun 8, 2022 · 1 comment

Comments

@TahaKh99
Copy link

TahaKh99 commented Jun 8, 2022

I am trying to send a single value to mysql DB but I am keep getting these errors.
So first of all this is my code:

`
var settings = new ConnectionSettings(
host: 'localhost',
port: 3306,
user: 'flutter_admin',
password: 'flutter_admin_password',
db: 'romicp'
);final conn = await MySqlConnection.connect(settings);

var result = await conn.query( 'insert into bluetoothvalues (taha) values (?)',
    [3.66]);
print('Inserted row id=${result.insertId}');

}
`

With this code, I get this error: Unhandled Exception: SocketException: Connection refused.
So I was looking for solutions and i found out that this question was asked before and it was fixed by changing 'localhost' to ''10.0.2.2'' or your IPaddress. both ways gave me another error which is: Connection timed out, host:

Please anyone can help

@ayoubzulfiqar
Copy link

class Mysql {
  // Note: if you are using terminal base connection than localhost is 127.0.0.1
  // Note if you are using fluter Emulator than localhost connection address is 10.0.2.2
  static String host = '10.0.2.2',
      user = 'root',
      // Note:- I am not using any password in my database
      // password = "123456789",

      // Database name that i created inside mySQL (Which should already exited)
      db = 'profiles';
  // Note:- MySQL sever port
  static int port = 8080;

  Mysql();

  Future<MySqlConnection> getConnection() async {
    var settings = ConnectionSettings(
      host: host,
      port: port,
      user: user,
      //  password: password,
      db: db,
    );
    return await MySqlConnection.connect(settings);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants