Reputation: 1404
I am trying to get an image from a JSON API, I made two links in the open data JSON, at this link: http://www.json-generator.com/api/json/get/bOEcLwbyqa?indent=2
Error: Another exception was thrown: HttpException: Connection closed while receiving data, uri =
For 'picturep' it Works (first screenshot), but for 'picture' it doesn't (second screenshot),
I'm using CicleAvatar with network image:
leading:CircleAvatar(
backgroundImage: NetworkImage(user.picturep),
),
The class:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class MyInherited extends StatefulWidget {
final Widget child;
MyInherited({this.child});
@override
MyInheritedState createState() => new MyInheritedState();
static MyInheritedState of(BuildContext context) {
return (context.inheritFromWidgetOfExactType(_MyInherited) as _MyInherited).data;
}
}
class MyInheritedState extends State<MyInherited> {
String _myField;
List<String> names;
// only expose a getter to prevent bad usage
String get myField => _myField;
void onMyFieldChange(String newValue) {
setState(() {
_myField = newValue;
});
}
Future<UsersList> fetchUser() async {
final response = await http.get('http://www.json-generator.com/api/json/get/bOEcLwbyqa?indent=2');
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
return UsersList.fromJson(json.decode(response.body));
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load post');
}
}
@override
Widget build(BuildContext context) {
return new _MyInherited(
data: this,
child: widget.child,
);
}
}
/// Only has MyInheritedState as field.
class _MyInherited extends InheritedWidget {
final MyInheritedState data;
_MyInherited({Key key, this.data, Widget child}) : super(key: key, child: child);
@override
bool updateShouldNotify(_MyInherited old) {
return true;
}
}
class UsersList {
final List<User> users;
UsersList({ this.users, });
factory UsersList.fromJson(List<dynamic> parsedJson) {
List<User> photos = new List<User>();
photos = parsedJson.map((i) => User.fromJson(i)).toList();
return new UsersList(users: photos);
}
}
class User {
String sId;
String name;
String email;
String picture;
String picturep;
String address;
int age;
String gender;
User(
{this.sId,
this.name,
this.email,
this.picture,
this.picturep,
this.address,
this.age,
this.gender});
User.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
name = json['name'];
email = json['email'];
picture = json['picture'];
picturep = json['picturep'];
address = json['address'];
age = json['age'];
gender = json['gender'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.sId;
data['name'] = this.name;
data['email'] = this.email;
data['picture'] = this.picture;
data['picturep'] = this.picturep;
data['address'] = this.address;
data['age'] = this.age;
data['gender'] = this.gender;
return data;
}
}
What I have tried.
Upvotes: 4
Views: 8172
Reputation: 2293
This is a new issue with the new Android studio update. Android studio HTTP proxy is enabled by default and this seems to cause the connection to disrupt for security reasons if the proxy deems the connection insecure (especially if it is an HTTP connection and not HTTPS connection). You have 2 options, to connect using HTTPS connection and make sure the connection is absolutely secured, or disable the android studio proxy configuration in your android emulator.
Option 2 is as follow:
Upvotes: 3