颤动:即使模型的数据结构正确,“ int”类型也不是“ String”类型错误的子类型

时间:2019-12-23 18:05:21

标签: arrays list flutter add provider

我有一个reward模型。如下所示:

class RewardModel {

  String id, status;
  DateTime createdAt, updatedAt;

  RewardModel({
    @required this.id,
    @required this.status,
    @required this.createdAt,
    @required this.updatedAt,
  });
}

在提供方,我有以下代码:


try {
        data['collection']['userData']['rewards'].forEach((result){
          print(result);
          _rewards.add(
            RewardModel(
              id: result['id'].toString(),
              status: status(result['status_id']),
              createdAt: DateTime.parse(result['created_at']),
              updatedAt: DateTime.parse(result['updated_at']),
            )
          ); 
        });
      } catch (e) {
        print(e);
      }


我确实从请求中获取数据,如下所示:

flutter: {id: 1, user_id: 1, status_id: 4, created_at: 2019-12-23 20:54:43, updated_at: 2019-12-23 20:54:43}
flutter: type 'int' is not a subtype of type 'String'

但是在下一行,我得到了错误。怎么了?

以下答案对我也没有用:

flutter http post "type 'int' is not a subtype of type 'String' in type cast"

type 'int' is not subtype of type 'String' on Flutter

1 个答案:

答案 0 :(得分:0)

当您将 int 值解析为 String 变量

时,会发生此错误

只需更改此行

来自

          status: status(result['status_id']),

          status: status(result['status_id'].toString()),

更改此行

来自

         String status

         int status