简体   繁体   中英

(Android Studio, Flutter) Connection timed out when trying to run my app using real device

I am running my app using real android device and in my flutter I used mysql1 to connect to localhost database. There is no problem when running the app on emulator but when comes to real device there is always a connection error. Below is my mysql.dart file:

/*This is a database configuration file*/

import 'package:mysql1/mysql1.dart';

class Mysql {

  static String host = '10.0.2.2',  
   

      user = 'root',  //User

      db = 'users';  //Database name

  static int port = 3306; //MySQL port number

  Mysql();

  Future<MySqlConnection> getConnection() async {

    var settings = new ConnectionSettings(

        host: host,
        port: port,
        user: user,
        db: db);

    return await MySqlConnection.connect(settings);

  }

}

I have tried a to change the host to my localhost ip and still getting the same error. Has anyone run into the same problem? 这是错误 -->

Instead of using mysql1,You should have to use sqflite in flutter https://pub.dev/packages/sqflite .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM