Home > database >  Flutter Unhandled Exception: type 'String' is not a subtype of type 'int' of �
Flutter Unhandled Exception: type 'String' is not a subtype of type 'int' of �

Time:10-28

I am getting null value from api with variable of storeUserID, in this variable i stored user id at the time of register but when i run app it shows null value. But when i manully type id like this https://aeliya.000webhostapp.com/demo.php?id=106764933065187174744 is shows me data.

//get users details
  Future<GetUserData> getUserDetail() async {
    var url = "https://aeliya.000webhostapp.com/demo.php?id=$storeUserID";
    var response = await http.get(Uri.parse(url));
    var data = jsonDecode(response.body.toString());

    if (response.statusCode == 200) {
      print(data);
      print(storeUserID);
      //print(data[0]['isAdmin']);
      return GetUserData.fromJson(data);
    } else {
      return GetUserData.fromJson(data);
    }
  }

at the same time i am getting following error.

E/flutter (13406): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index' E/flutter (13406): #0 new GetUserData.fromJson (package:mahuva_azadari/Models/GetUserData.dart:19:17) E/flutter (13406): #1 _AdminReqState.getUserDetail (package:mahuva_azadari/Screens/Admin Request.dart:352:26) E/flutter (13406):

Following is my response:

 [{"name":"Taki Rajani","email":"[email protected]","isAdmin":"0","description":"testing "}]

GetUserData

/// name : "Taki Rajani"
/// email : "[email protected]"
/// isAdmin : "0"
/// description : "testing "

class GetUserData {
  GetUserData({
      String? name, 
      String? email, 
      String? isAdmin, 
      String? description,}){
    _name = name;
    _email = email;
    _isAdmin = isAdmin;
    _description = description;
}

  GetUserData.fromJson(dynamic json) {
    _name = json['name'];
    _email = json['email'];
    _isAdmin = json['isAdmin'];
    _description = json['description'];
  }
  String? _name;
  String? _email;
  String? _isAdmin;
  String? _description;
GetUserData copyWith({  String? name,
  String? email,
  String? isAdmin,
  String? description,
}) => GetUserData(  name: name ?? _name,
  email: email ?? _email,
  isAdmin: isAdmin ?? _isAdmin,
  description: description ?? _description,
);
  String? get name => _name;
  String? get email => _email;
  String? get isAdmin => _isAdmin;
  String? get description => _description;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['name'] = _name;
    map['email'] = _email;
    map['isAdmin'] = _isAdmin;
    map['description'] = _description;
    return map;
  }

}

CodePudding user response:

so I see the error in Models/GetUserData.dart I think in your model "isAdmin":"0" you save it as int but it is returning from the api as String

CodePudding user response:

Change this if your response getting int value

_isAdmin = json['isAdmin'].toString();
  • Related