简体   繁体   English

Flutter 问题:在真实设备上运行时,与 postgresql 数据库的连接不起作用

[英]Flutter Problem: connection with postgresql database not working when running on real device

I got a problem when running my flutter app on my phone while connecting with postgresql database and the terminal is not showing any error.我在连接 postgresql 数据库时在手机上运行 flutter 应用程序时遇到问题,并且终端没有显示任何错误。 PS: when I run the app on an emulator it's working fine. PS:当我在模拟器上运行应用程序时,它工作正常。 any help please??请问有什么帮助吗??

signIn(String username, password) async {
    SharedPreferences.setMockInitialValues({});
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

    Map data = {
      'username': username,
      'password': password
    };
    var ur = Uri.parse('http://192.168.137.5:8000/api/token/auth/'); 
    var jsonResponse;
    var response = await http.post(ur, body: data);
    print(response.body);
    var user = response.body;
    if(response.statusCode == 200) {

      jsonResponse = json.decode(response.body);
      
      if(jsonResponse != null) {
        setState(() {
          _isLoading = false;
        });
        
        sharedPreferences.setString("token", jsonResponse['token']);
        sharedPreferences.setString("username", jsonResponse['username']);
        sharedPreferences.setInt("user_id", jsonResponse['user_id']);
        int id =sharedPreferences.getInt('user_id');
        
        final connection = PostgreSQLConnection("192.168.137.5", 5432, "aaa", username: "aaaa", password: "aaa");
        
        
        await connection.open();
        print('connected');
        var result = await
        connection.query("SELECT * FROM public.employee_employee, public.auth_user where public.employee_employee.user_id= ${id}");

        print(result);
        
        sharedPreferences.setString("nom", result[0][7]);
        Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => Home()), (Route<dynamic> route) => false);
       await connection.close();
      }
    }
    else {
      setState(() {
        _isLoading = false;
      });
      print(response.body.characters);
    }
  }

It is just because your phone and laptop are not connected with same connection or you can run your app on emulator instead of real device or you can push your code on testing server like heroku then it will work perfectly.这只是因为您的手机和笔记本电脑没有连接相同的连接,或者您可以在模拟器而不是真实设备上运行您的应用程序,或者您可以将您的代码推送到 heroku等测试服务器上,那么它将完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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