0

I want to have python backend for my flutter app. So I created the python file:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/", methods = ["GET"])
def index():
    return jsonify({"testForMe": "HI! THIS IS PYTHON"})

if __name__ == "__main__":
    app.run(debug = True)

This is connected to my main.dart file:

            Text(
              greetings,
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            Center(
              child: Container(
                width: 150,
                height: 60,
                child: FlatButton(
                  color: Colors.blue,
                  onPressed: () async {
                    final antwort = await http
                        .get("http://127.0.0.1:5000/")
                        .timeout(const Duration(seconds: 3));
                    print("HI");
                    final decoded =
                        json.decode(antwort.body) as Map<String, dynamic>;

                    setState(() {
                      greetings = decoded["testForMe"];
                    });
                    greetings += "hi";
                    (context as Element).reassemble();
                  },
                  child: Text(
                    "Press",
                    style: TextStyle(
                      fontSize: 24,
                    ),
                  ),
                ),
              ),
            )

But after pressing the created Button, nothing happens and there is the error:

E/flutter ( 8706): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 40732
E/flutter ( 8706):

I found anybody with the same problem here Flutter Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111 but there was not a real solution for the problem.

Anyone knows, what to do now?

Thanks

lulu
  • 9
  • 2

1 Answers1

0

change the localhost to http://10.0.2.2:port_number in your case http://10.0.2.2:40732

  • You welcome, because android emulator uses this address for pointing to localhost – kadhum alrubaye Feb 15 '21 at 21:50
  • Hi, I have same issue. Can you check this https://stackoverflow.com/questions/67649509/unable-to-connect-flutter-with-flask-backend-on-local-host –  May 22 '21 at 12:33