如何解决抖动错误字符串不是int类型的子类型

时间:2019-09-09 19:20:53

标签: string flutter dart int

我正在尝试学习开发移动应用程序的方法。并通过github下载了用于登录系统的示例项目。在这里;

https://github.com/vemarav/auth_flow

在此示例中,我使用我的api和Im更改了api和终结点;

{status: 1, message: User login successfully, data: {user_id: 3, name: user_name, email_id: user@email.com, password: *****, image: http://image.url, address: New York, office_address: Charleston & Huff, Mountain View, CA 94043, USA, live_lat: 41.2695887, live_long: -73.0302559, role: 2, status: 1, approval_status: 1, created_at: 1545317082, mobile: 0905375933055, referral_code: CKM676, user_referral_code: , gender: 1, city: new york, country: usa, updated_at: 1545317082, device_type: device_type, device_id: device_id, device_token: device_token, latitude: 37.4207325, longitude: -122.08313869999999, i_card: http://image.url, country_code: , mobile_no: , bank_name: , account_no: , ifsc_code: , account_holder_name: }}

但是在auth_utils.dart页面中的响应之后,我已将其修改为我的要求。进行更改后,外观如下所示。

var user = response['user'];
prefs.setInt(userIdKey, user['user_id']);
prefs.setString(nameKey, user['name']);

这些更改之后,我得到此错误;

Unhandled Exception: type 'String' is not a subtype of type 'int'. 

也许这很容易,但是我是扑扑/飞镖的新手,这对我来说有点复杂。

我尝试将响应转换为int(也许是string),但此后出现此错误;

The method 'setInt' was called on null

感谢您的回复

---------------编辑---------------

我已经检查了两个API的响应,我的响应和github中的源之间有区别。 github响应上的代码为 int ,我的响应为 string 。这就是为什么我试图用它来解析;

prefs.setInt(userIdKey, int.parse(user['user_id']));

这些代码之后,我得到这些错误

Unhandled Exception: NoSuchMethodError: The method 'setInt' was called on null. Receiver: null Tried calling: setInt("user_id", 3)

但这不是不为空

现在我已经测试了prefs.setString(nameKy, user['name']),并且给出了相同的错误。但这也是不为空

1 个答案:

答案 0 :(得分:0)

您必须在setInt中输入null,并在调用此方法之前进行处理。

尝试

prefs.setInt('userIdKey', user['user_id'] != null ? user['user_id'] : 0);

    prefs.setInt('userIdKey', user['user_id'] ?? 0);
相关问题