My debug statement is displaying the database correctly, by after something is going wrong and in getting the error: Unhandled Exception: NoSuchMethodError: Class 'int' has no instance method '[]'. I am getting null data when I'm trying to display is in my application, this is because the list i am using to store the values has length 0
following is my class
class UserDetails extends ChangeNotifier {
final String? userId;
final String? mobileno;
bool? userStatus;
final String? adhar;
final String? pan;
final String? adharno;
final String? panno;
UserDetails(
{this.userId,
this.mobileno,
this.userStatus = false,
this.adhar,
this.pan,
this.adharno,
this.panno});
}
following is my api where I am facing error
Future<void> getUsers(BuildContext context) async {
final url = '${PurohitApi().baseUrl}${PurohitApi().users}';
final List<UserDetails> loadedUsers = [];
try {
final client = RetryClient(
http.Client(),
retries: 4,
when: (response) {
return response.statusCode == 401 ? true : false;
},
onRetry: (req, res, retryCount) async {
//print('retry started $token');
if (retryCount == 0 && res?.statusCode == 401) {
var accessToken = await Provider.of<Auth>(context, listen: false)
.restoreAccessToken();
// Only this block can run (once) until done
req.headers['Authorization'] = accessToken;
}
},
);
var response = await client.get(
Uri.parse(url),
headers: {'Authorization': authToken!},
);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
print(extractedData);
if (extractedData['data'] == null) {
return;
}
extractedData.forEach((userId, userData) {
print(userId);
loadedUsers.add(
UserDetails(
userId: userData['data']['id'],
mobileno: userData['data']['mobileno'],
userStatus: userData['data']['userstatus'],
adhar: userData['data']['adhar'],
pan: userData['data']['pan'],
adharno: userData['data']['adharno'],
panno: userData['data']['panno'],
),
);
});
_users = loadedUsers.reversed.toList();
//print(users);
notifyListeners();
} catch (e) {
print(e);
}
}
}
I am facing NoSuchMethodError: Class 'int' has no instance method '[]'Tried calling: from above api following is my response
CodePudding user response:
I think userId should not be String, It should be an integer
class UserDetails extends ChangeNotifier {
final int? userId;
final String? mobileno;
bool? userStatus;
final String? adhar;
final String? pan;
final String? adharno;
final String? panno;
UserDetails(
{this.userId,
this.mobileno,
this.userStatus = false,
this.adhar,
this.pan,
this.adharno,
this.panno});
}
CodePudding user response:
try to print your data in this error I think your data should be like userData['id'],
the above error is you try to use int as an object in the data returned
the returned userData is an int
CodePudding user response:
change this code :
extractedData.forEach((userId, userData) {
print(userId);
loadedUsers.add(
UserDetails(
userId: userData['data']['id'],
mobileno: userData['data']['mobileno'],
userStatus: userData['data']['userstatus'],
adhar: userData['data']['adhar'],
pan: userData['data']['pan'],
adharno: userData['data']['adharno'],
panno: userData['data']['panno'],
),
);
});
to this :
loadedUsers.add(
UserDetails(
userId: extractedData['data']['id'],
mobileno: extractedData['data']['mobileno'],
userStatus: extractedData['data']['userstatus'],
adhar: extractedData['data']['adhar'],
pan: extractedData['data']['pan'],
adharno: extractedData['data']['adharno'],
panno: extractedData['data']['panno'],
),
);
the reason why :
extractedData
is a map<String,dynamic>
, when you call function forEach(key, value)
:
the value is not a map, is an Object
, or dynamic
. the key is a String
.
when you call this fuction :
value['data]
its will throw that error
if you extractedData
is a List of Map, you can keep you original code, and change type of extractedData to List :
final extractedData = json.decode(response.body) as List<Map<String, dynamic>>;
Edit : your data in the response is a list Of a map so the code will be this :
final extractedData = json.decode(response.body) as Map<String,dynamic>;
List<Map<String,dynamic>> data = extractedData['data'];
for (var map in data){
loadedUsers.add(
UserDetails(
userId: map['id'],
mobileno: map['mobileno'],
userStatus: map['userstatus'],
adhar: map['adhar'],
pan: map['pan'],
adharno: map['adharno'],
panno: map['panno'],
),
);
}