In my flutter project I use "mysql1" package to connect with a mysql server, I tried it with an online server and it worked good.
but when I tried to use it in a local server it didn’t work but don’t know what is the real problem.
here is the class which called mysql1 package
import 'package:mysql1/mysql1.dart';
class MySqlDB {
static String host = 'localhost',
user = 'root',
password = 'admin',
db = 'univ';
static int port = 3306;
Future<MySqlConnection> getConnection() async{
var settings = new ConnectionSettings(
host: host,
user: user,
password: password,
port: port,
db: db
);
return await MySqlConnection.connect(settings);
}
}
And here is the problem message:
E/flutter ( 5603): [ERROR:flutter/lib/ui/ui_dart_state.cc(213)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 59476
E/flutter ( 5603):
"update :
I use Xampp app to run apache & MySQL and phpmyadmin, so without this app there is no mysql on the laptop.
The purpos of my project is just to run this app on the emulator."
This image has myphp admin details:
So..
How can I fix it?
I’ve tried many ways and see lot’s of posts on stackoverflow but with no advanteges!!
3
Answers
If the database is not running on the Android device but on a computer on your local network. You need to replace "localhost" with the IP address of that system.
For example if the IP address of the computer running the MySQL database is 192.168.1.99, change the line:
to
Here :
static String host = 'localhost'
on emulator use your PC name
PC1
for exampleAfter long time of trying with this problem, I see that connecting to a local server by an android emulator isn’t good way to try.
The logical way is to connect by an online server.