Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Http error SocketException: OS Error: Connection refused

I'm using the last flutter version on a fresh created project. I'm trying to call this URL https://jsonplaceholder.typicode.com/users

But on iOS or Android I get flutter: Error SocketException: OS Error: Connection refused, errno = 61, address = jsonplaceholder.typicode.com, port = 52988

Here is my network call:

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:test_flutter/constants.dart';
import 'package:test_flutter/users/models/user.dart';

class UserNetworkDatasource {
  Future<List<User>> retrieve() async {
    var httpClient = HttpClient();
    var uri = new Uri.https(baseUrl, '/users');
    var request = await httpClient.getUrl(uri);
    var response = await request.close();
    var responseJson = await response.transform(utf8.decoder).join();
    List userMap = json.decode(responseJson);

    return userMap.map((jsonUser) => User.fromJson(jsonUser));
  }
}

Is there anything to do more than this ? I check the Android manifest and it has the Internet permission so should be ok

Flutter 0.3.2 • channel beta

Framework • revision 44b7e7d3f4 (4 weeks ago) • 2018-04-20 01:02:44 -0700

Engine • revision 09d05a3891

Tools • Dart 2.0.0-dev.48.0.flutter-fe606f890b

like image 478
jaumard Avatar asked May 16 '18 11:05

jaumard


2 Answers

I had the same error, but only on release build (android). In the android folder under app/src are 3 folders: debug, main and profile, each containing AndroidManifest file. The one in debug folder had internet permission, but the one in main did not and that was causing the error.

like image 174
Ethirallan Avatar answered Sep 18 '22 07:09

Ethirallan


Try to go to the url from the phone. I had the same issue, I was using python http.server for hosting a json file. First I was giving me the same exception, because I bind it with a predefined url. And my emulator couldn't reach the url.

like image 30
Ritam Chakraborty Avatar answered Sep 22 '22 07:09

Ritam Chakraborty