0

I have my phone connected and I'm trying to simply fetch some records as is shown in this tutorial: https://www.youtube.com/watch?v=ig6WRq73iEg&ab_channel=JesusHedo

mysql.dart

import 'package:mysql1/mysql1.dart';

class Mysql {
  static String host = 'localhost',
                user = 'root',
                password = '',
                db = 'sat_data';
  static int port = 3306;

  Mysql();

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

main.dart

import 'package:flutter/material.dart';
import 'package:untitled/logic/mysql.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  var db = new Mysql();
  var username = "";

  void getCustomer() {
    db.getConnection().then((conn) {
      String sql = 'select username from users where id=46;';
      conn.query(sql).then((results) {
        for(var row in results) {
          setState(() {
            username = row[1];
          });
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Your username is: ',
            ),
            Text(
              '$username',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: getCustomer,
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

However, I'm getting following error:

E/flutter (27925): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 49986

click again button again..

E/flutter (27925): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 50002

I read couple of examples: Flutter Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111

SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend

also tried adding <uses-permission android:name="android.permission.INTERNET" />, but nothing works.

  • Looks OK, but since you get a Connection refused I would make sure to double check that you actually have a mysql server running on localhost with those credentials. It would help to see the complete console output. I think if you are using a device or emulator they do not have the same IP as your mysql server because it is probably running on your development computer. – Andreas Toresäter Nov 11 '21 at 10:47

0 Answers0