【问题标题】:Unhandled Exception: type 'String' is not a subtype of type 'int'未处理的异常:“String”类型不是“int”类型的子类型
【发布时间】:2020-03-04 19:05:31
【问题描述】:

需要帮助来解决这个问题,我正在尝试学习颤振并在运行测试 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);
  }

}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我认为问题出现是因为您试图将作为字符串的 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']);
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2021-07-08
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多