dart 另一个异常被抛出:HttpException:Connection closed while receiving data,uri =

jgovgodb  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(161)

我试图从JSON API中获取图像,我在开放数据JSON中做了两个链接,在此链接:http://www.json-generator.com/api/json/get/bOEcLwbyqa?indent=2
错误:另一个异常被抛出:HttpException:Connection closed while receiving data,uri =
对于“picturep”它工作(第一个屏幕截图),但对于“picture”它不工作(第二个屏幕截图),
我正在使用CicleAvatar与网络图像:

leading:CircleAvatar(
          backgroundImage: NetworkImage(user.picturep),
    ),

字符串
班级:

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;
      }
    }


我已经尽力了。
100d1x

的字符串

jckbn6z7

jckbn6z71#

这是新的Android Studio更新中的一个新问题。默认情况下,Android Studio HTTP代理处于启用状态,如果代理认为连接不安全,这似乎会导致连接中断(特别是如果它是一个HTTP连接,而不是HTTPS连接).你有2个选项,连接使用HTTPS连接,并确保连接是绝对安全的,或者在你的android模拟器中禁用android studio代理配置。
备选案文2如下:


的数据


hjqgdpho

hjqgdpho2#

检查你的图片是否有一个有效的图片URL。

相关问题