0

I've created a simple flutter app which will get data from an api in json format sent from the backend (Python flask) and print out the data on click of a button.

When I click on the button, I get the error

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

I'm very new to flutter, please tell me where I did wrong and help me figure it out.

Flutter Dart code:

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(new MaterialApp(
  home: new HomePage(),
));
}

class HomePage extends StatefulWidget {
@override
HomePageState createState() => new HomePageState();
}

class HomePageState extends State<HomePage> {

List data;

Future<String> getData() async {
var response = await http.get(
  Uri.encodeFull("http://127.0.0.1:5000/ "),
  headers: {
    "Accept": "application/json"
  }
);
data = json.decode(response.body);
print(data);

return "Success!";
}

@override
Widget build(BuildContext context) {
 return new Scaffold(
   body: new Center(
     child: new RaisedButton(
       child: new Text("Get data"),
       onPressed: getData,
     ),
   ),
 );
}
}

Python code:

from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/')
def hello_world():
return jsonify({"about": "Hello World!"})

if __name__ == '__main__':
app.run()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kavishka Rajapakshe
  • 537
  • 1
  • 8
  • 23
  • 1
    I believe the problem can be in this line `Uri.encodeFull("http://127.0.0.1:5000/ ")`. The address you are pointing to can be wrong if you are running Emulator. If it is an Android Emulator - try to change the IP address to `10.0.2.2`. Additionally, you can try to run `ifconfig` and try received IP address from there, e.g. `192.168.0.109` – therceman Mar 18 '21 at 16:12

1 Answers1

0

Looks like you are running your back-end from localhost. In this case, 127.0.0.1 will only work if you run the application on your PC (and not another phone/device. Not sure about simulators though).

Trying to access 127.0.0.1 from a phone is like trying to access a server hosted on your phone (which evidently will not be found).

So, to run the app on your phone, you need to find your PC's (private) IP address, using e.g. ifconfig from your terminal. Once you get this IP address, use the URL {your-priv-IP}:5000 instead of 127.0.0.1:5000 in your code. Make sure though that your PC and phone are connected to the same Wifi network for this to work.

Anis R.
  • 6,656
  • 2
  • 15
  • 37
  • I tried what you said,But it didnt do anything... Im running my app on my android studio emulator .Should I just replace the address in m flutter code with my ipv4 address? – Kavishka Rajapakshe Mar 18 '21 at 17:04
  • A comment by rceman suggests using 10.0.2.2 in case of an android simulator, I guess you can give it a try. So, something like `http://10.0.2.2:5000` – Anis R. Mar 18 '21 at 20:30