需要帮助解决此问题,我试图学习颤振并在运行测试apk时被卡住。它只是显示错误未处理的异常:类型'String'不是类型'int'的子类型。我尝试过但是没有运气 这是我的代码
class Login {
final String email;
final String password;
Login(this.email,this.password);
Future<bool> getData(BuildContext context) async {
bool isSuccessed;
int id;
int rule;
String image;
Response response = await get(InfixApi.login(email, password));
if (response.statusCode == 200) {
Map<String, dynamic> user = jsonDecode(response.body) as Map;
isSuccessed = user['success'];
id = user['data']['user']['id'];
rule = user['data']['user']['role_id'];
image = user['data']['userDetails']['staff_photo'];
if (isSuccessed) {
saveBooleanValue('isLogged', isSuccessed);
saveStringValue('email', email);
saveStringValue('password', password);
saveStringValue('id','$id');
saveStringValue('rule', '$rule');
saveStringValue('image', '$image');
saveStringValue('lang', 'en');
AppFunction.getFunctions(context,rule.toString());
}
}
return isSuccessed;
}
Future<bool> saveBooleanValue(String key ,bool value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setBool(key, value);
}
Future<bool> saveStringValue(String key ,String value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setString(key, value);
}
}
答案 0 :(得分:0)
我认为问题来了,因为您试图将ID和规则作为字符串存储在int变量中,请尝试替换这两行:
id = user['data']['user']['id'];
rule = user['data']['user']['role_id'];
id = int.parse(user['data']['user']['id']);
rule = int.parse(user['data']['user']['role_id']);
希望有帮助