Unhandled Exception: SocketException: Connection timed out
Bonjour,
J'apprends le DART, et j'utilise le connector MySQL "mysql1".
J'ai l'erreur suivante :
Citation:
E/flutter (32240): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: SocketException: Connection timed out, host: localhost, port: 3306
E/flutter (32240): #0 _NativeSocket.connect.<anonymous closure>.<anonymous closure> (dart:io-patch/socket_patch.dart:988:11)
E/flutter (32240): #1 _RootZone.run (dart:async/zone.dart:1655:54)
main.dart :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| import 'package:flutter/material.dart';
import 'connectionDatabase.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Page d\'accueil'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var db = ConnectionDatabase();
var name = '';
void _getCustomer(){
print('_getCustomer');
db.getConnection().then((conn) {
String sql = 'SELECT name FROM users WHERE id=1;';
print('_getCustomer2');
conn.query(sql).then((results){
for(var row in results) {
setState(() {
name = row[0];
print('The value of the input is: $name');
});
}
});
conn.close();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Name :',
),
Text(
name,
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _getCustomer,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
} |
connectionDatabase.dart :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import 'dart:async';
import 'package:mysql1/mysql1.dart';
class ConnectionDatabase {
static String host = 'localhost',
user = 'chopetondiplome',
password = "chopetondiplome",
db = 'chopetondiplome';
static int port = 3306;
ConnectionDatabase();
Future<MySqlConnection> getConnection() async {
var settings = ConnectionSettings(
host: host,
port: port,
user: user,
password: password,
db: db
);
print(host);
return await MySqlConnection.connect(settings);
}
} |
Une idée du problème ? merci