我无法从 API 中获取值

I cannot get values from API in flutter

我不明白为什么它不起作用?当我在另一个列表中使用它时,它起作用了,但这次不起作用。

Future<UserResponse> login() async {
    Map<String, String> headers = {};
    headers["Content-Type"] =
    "application/json; charset=UTF-8";
    final response = await http.get(Uri.parse('http://myserverip/api/Most/Login?username=demo&pass=123456'),
        headers: headers);
    debugPrint('response\n'+response.body.toString());
    final jsonItems = json.decode(response.body).cast<Map<String,dynamic>>();
    debugPrint('json\n'+jsonItems.toString());
    UserResponse resp = jsonItems.object<UserResponse>((json){
      debugPrint('asdasd\n'+UserResponse.fromJson(json).toString());
      return UserResponse.fromJson(json);
    });
    debugPrint('aftercast\n'+resp.locationName.toString());
    return resp;
  }

这是我的输出 json 但我无法将其与我的 class 匹配:

[
    {
        "id": 1,
        "userName": "demo",
        "userPassword": "123456",
        "isOnline": false,
        "isActive": true,
        "superVisor": false,
        "createdDate": "2022-01-28T16:23:40.07",
        "updatedDate": "2022-01-28T16:23:40.07",
        "locationName": "Doğanlar",
        "address": "Izmir",
        "dbhostName": "myserverip",
        "dbcatalogName": "Place1",
        "dbuserId": "sa",
        "dbpassword": "Most123456",
        "locationCreateDate": "2022-01-28T15:54:53.887",
        "companyName": "Company1",
        "authName": "FullAccess",
        "unitName": "Production",
        "screenName": "Production",
        "unitId": 1,
        "screenId": 1,
        "locationId": 1,
        "companyId": 1,
        "authId": 5
    }
]

这是我的用户响应Class。我认为从这里开始没有问题。

class UserResponse {
  int? id;
  String? userName;
  String? userPassword;
  bool? isOnline;
  bool? isActive;
  bool? superVisor;
  String? createdDate;
  String? updatedDate;
  String? locationName;
  String? address;
  String? dbhostName;
  String? dbcatalogName;
  String? dbuserId;
  String? dbpassword;
  String? locationCreateDate;
  String? companyName;
  String? authName;
  String? unitName;
  String? screenName;
  int? unitId;
  int? screenId;
  int? locationId;
  int? companyId;
  int? authId;

  UserResponse({this.id,
        this.userName,
        this.userPassword,
        this.isOnline,
        this.isActive,
        this.superVisor,
        this.createdDate,
        this.updatedDate,
        this.locationName,
        this.address,
        this.dbhostName,
        this.dbcatalogName,
        this.dbuserId,
        this.dbpassword,
        this.locationCreateDate,
        this.companyName,
        this.authName,
        this.unitName,
        this.screenName,
        this.unitId,
        this.screenId,
        this.locationId,
        this.companyId,
        this.authId});

  factory UserResponse.fromJson(Map<String, dynamic> json) {
    return UserResponse(
    id : json['id'],
    userName : json['userName'],
    userPassword : json['userPassword'],
    isOnline : json['isOnline'],
    isActive : json['isActive'],
    superVisor : json['superVisor'],
    createdDate : json['createdDate'],
    updatedDate : json['updatedDate'],
    locationName : json['locationName'],
    address : json['address'],
    dbhostName : json['dbhostName'],
    dbcatalogName : json['dbcatalogName'],
    dbuserId : json['dbuserId'],
    dbpassword : json['dbpassword'],
    locationCreateDate : json['locationCreateDate'],
    companyName : json['companyName'],
    authName : json['authName'],
    unitName : json['unitName'],
    screenName : json['screenName'],
    unitId : json['unitId'],
    screenId : json['screenId'],
    locationId : json['locationId'],
    companyId : json['companyId'],
    authId : json['authId'],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['userName'] = this.userName;
    data['userPassword'] = this.userPassword;
    data['isOnline'] = this.isOnline;
    data['isActive'] = this.isActive;
    data['superVisor'] = this.superVisor;
    data['createdDate'] = this.createdDate;
    data['updatedDate'] = this.updatedDate;
    data['locationName'] = this.locationName;
    data['address'] = this.address;
    data['dbhostName'] = this.dbhostName;
    data['dbcatalogName'] = this.dbcatalogName;
    data['dbuserId'] = this.dbuserId;
    data['dbpassword'] = this.dbpassword;
    data['locationCreateDate'] = this.locationCreateDate;
    data['companyName'] = this.companyName;
    data['authName'] = this.authName;
    data['unitName'] = this.unitName;
    data['screenName'] = this.screenName;
    data['unitId'] = this.unitId;
    data['screenId'] = this.screenId;
    data['locationId'] = this.locationId;
    data['companyId'] = this.companyId;
    data['authId'] = this.authId;
    return data;
  }
}

未来的问题在哪里?我猜想在铸造方面存在实施问题。你能帮帮我吗?

试试这个:

Future<UserResponse> login() async {
    Map<String, String> headers = {};
    headers["Content-Type"] =
    "application/json; charset=UTF-8";
    final response = await http.get(Uri.parse('http://myserverip/api/Most/Login?username=demo&pass=123456'),
        headers: headers);
    final jsonItems = json.decode(response.body);
    return UserResponse.fromJson(jsonItems);
}

将您的 Future 方法更改为此。

Future<List<UserResponse>> login() async {
    Map<String, String> headers = {};
    headers["Content-Type"] = "application/json; charset=UTF-8";
    final response = await http.get(
        Uri.parse('http://myserverip/api/Most/Login?username=demo&pass=123456'),
        headers: headers);
    debugPrint('response\n' + response.body.toString());
    var jsonItems = json.decode(response.body);
    debugPrint('json\n' + jsonItems.toString());
    List<UserResponse> resp =
        (jsonItems as List).map((e) => UserResponse.fromJson(e)).toList();
    // debugPrint('aftercast\n' + resp[0]!.locationName!.toString());
    return resp;
  }

您可以像这样访问响应。

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: login(),
      builder: (context, AsyncSnapshot<List<UserResponse>> snapshot) {
        if (snapshot.hasError) {
          return const Center(
            child: Text('Error'),
          );
        } else if (snapshot.hasData) {
          debugPrint(snapshot.data.toString());
          return Center(
            child: Text(snapshot.data![0].locationName!),
          );
        } else {
          return const CircularProgressIndicator();
        }
      },
    );
  }