flutter 类型“int”不是类型“double”的子类型?'在列表视图中

g9icjywg  于 2023-05-23  发布在  Flutter
关注(0)|答案(1)|浏览(109)

我上了下面的美食模特课

class FoodModel {
  int? foodId;
  String? foodNameEn;
  String? foodNameAr;
  double? kcal;
  double? fats;
  double? carbs;
  double? protein;
  String? createTime;
  int? foodTypeId;
  String? foodImage;

  FoodModel(
      {this.foodId,
      this.foodNameEn,
      this.foodNameAr,
      this.kcal,
      this.fats,
      this.carbs,
      this.protein,
      this.createTime,
      this.foodTypeId,
      this.foodImage});

  FoodModel.fromJson(Map<String, dynamic> json) {
    foodId = json['food_id'];
    foodNameEn = json['food_name_en'];
    foodNameAr = json['food_name_ar'];
    kcal = json['kcal'];
    fats = json['fats'];
    carbs = json['carbs'];
    protein = json['protein'];
    createTime = json['create_time'];
    foodTypeId = json['food_type_id'];
    foodImage = json['food_image'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['food_id'] = foodId;
    data['food_name_en'] = foodNameEn;
    data['food_name_ar'] = foodNameAr;
    data['kcal'] = kcal;
    data['fats'] = fats;
    data['carbs'] = carbs;
    data['protein'] = protein;
    data['create_time'] = createTime;
    data['food_type_id'] = foodTypeId;
    data['food_image'] = foodImage;
    return data;
  }
}

我正在尝试实现一个列表视图

class FoodList extends GetView<FoodControllerImp> {
  const FoodList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 15,vertical: 10),
      height: Get.height,
      child: ListView.builder(
        physics: BouncingScrollPhysics(),
          //separatorBuilder: (context, index) => const SizedBox(width: 10),
          itemCount: controller.food.length,
          scrollDirection: Axis.vertical,
          itemBuilder: (context, index) {
            return Foods(
              i: index,
              foodModel:
                  FoodModel.fromJson(controller.food[index]),
            );
          },
        ),
    );
  }
}

class Foods extends GetView<FoodControllerImp> {
  final FoodModel foodModel;
  final num? i;
  const Foods({Key? key, required this.foodModel, required this.i})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10,horizontal: 10),
      margin: EdgeInsets.symmetric(vertical: 10),
      height: 140,
      decoration: BoxDecoration(
        border: Border.all(
          color: Colors.grey.withOpacity(0.5)
        ),
        borderRadius: BorderRadius.circular(30)
      ),
      child: GestureDetector(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
            children: [
                   ]),
            onTap: (){
              controller.goToFoodDetail(foodModel);
            },
      ),
    );
  }
}

但是它一直给我一个错误,说类型'int'不是类型'double的子类型?'

以前,当thunder客户端请求都是字符串时,它工作得很好,但我修复了它,使它返回数据库中实现的所有内容

7fyelxc5

7fyelxc51#

基于@Creator评论
只需将int和double更改为num

class FoodModel {
  int? foodId;
  String? foodNameEn;
  String? foodNameAr;
  num? kcal;
  num? fats;
  num? carbs;
  num? protein;
  String? createTime;
  int? foodTypeId;
  String? foodImage;

相关问题