0

I cannot figure it out why it is not working? When i used this in another list it works but this time does not.

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

This is my output which is json but i cannot match it with my 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
    }
]

This is my UserResponse Class. I think there is no problem from here.

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

Where is the problem of the future? I guess there is an implementation problem for example in casting. Could you help me?

1
  • please provide UserResponse class too. thanks Commented Feb 1, 2022 at 8:16

2 Answers 2

1

Change your Future method to this.

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

you can access the response like this.

  @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();
        }
      },
    );
  }

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you bro how i much i need this, you cannot imagine
Glad i was able to help.
0

try this:

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

1 Comment

Thanks for your effort but it does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.