如何解决flutter报错String is not a subtype of type int
How to solve flutter error String is not a subtype of type int
我正在尝试学习 flutter 来开发移动应用程序。超过 github 我已经下载了一个登录系统的示例项目。在这里;
https://github.com/vemarav/auth_flow
在这个示例中,我用 api 更改了 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'.
也许这很简单,但我是 flutter/dart 的新手,对我来说有点复杂。
我曾尝试将响应转换为 int(可能是字符串),但之后出现此错误;
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'])
并且给出了相同的错误。但它也 not null。
您必须在 setInt
中输入 null,然后再调用此方法。
试一试
prefs.setInt('userIdKey', user['user_id'] != null ? user['user_id'] : 0);
或者
prefs.setInt('userIdKey', user['user_id'] ?? 0);
我正在尝试学习 flutter 来开发移动应用程序。超过 github 我已经下载了一个登录系统的示例项目。在这里;
https://github.com/vemarav/auth_flow
在这个示例中,我用 api 更改了 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'.
也许这很简单,但我是 flutter/dart 的新手,对我来说有点复杂。
我曾尝试将响应转换为 int(可能是字符串),但之后出现此错误;
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'])
并且给出了相同的错误。但它也 not null。
您必须在 setInt
中输入 null,然后再调用此方法。
试一试
prefs.setInt('userIdKey', user['user_id'] != null ? user['user_id'] : 0);
或者
prefs.setInt('userIdKey', user['user_id'] ?? 0);