转HomeMo一直出错:type 'Future<dynamic>' is not a subtype of type 'HomeMo'

来源:6-2 首页接口系分与Dao层实现

qq_稻草人_5

2023-01-02

转HomeMo一直出错:
错误信息如下:
type ‘Future’ is not a subtype of type ‘HomeMo’

出错位置:
图片描述
HomeMo代码如下:

import 'package:bili/model/video_model.dart';

///解放生产力:在线json转dart https://www.devio.org/io/tools/json-to-dart/
class HomeMo {
  List<BannerMo>? bannerList;
  List<CategoryMo>? categoryList;
  late List<VideoModel>? videoList;

  HomeMo({this.bannerList, this.categoryList, required this.videoList});

  HomeMo.fromJson(Map<String, dynamic> json) {
    if (json['bannerList'] != null) {
      bannerList = new List<BannerMo>.empty(growable: true);
      json['bannerList'].forEach((v) {
        bannerList!.add(new BannerMo.fromJson(v));
      });
    }
    if (json['categoryList'] != null) {
      categoryList = new List<CategoryMo>.empty(growable: true);
      json['categoryList'].forEach((v) {
        categoryList!.add(new CategoryMo.fromJson(v));
      });
    }
    if (json['videoList'] != null) {
      videoList = new List<VideoModel>.empty(growable: true);
      json['videoList'].forEach((v) {
        videoList?.add(new VideoModel.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.bannerList != null) {
      data['bannerList'] = this.bannerList!.map((v) => v.toJson()).toList();
    }
    if (this.categoryList != null) {
      data['categoryList'] = this.categoryList!.map((v) => v.toJson()).toList();
    }
    // data['videoList'] = this.videoList.map((v) => v.toJson()).toList();
    return data;
  }
}

class BannerMo {
  late String id;
  late int sticky;
  late String type;
  late String title;
  late String subtitle;
  late String url;
  late String cover;
  late String createTime;

  BannerMo.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    sticky = json['sticky'];
    type = json['type'];
    title = json['title'];
    subtitle = json['subtitle'];
    url = json['url'];
    cover = json['cover'];
    createTime = json['createTime'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['sticky'] = this.sticky;
    data['type'] = this.type;
    data['title'] = this.title;
    data['subtitle'] = this.subtitle;
    data['url'] = this.url;
    data['cover'] = this.cover;
    data['createTime'] = this.createTime;
    return data;
  }
}

class CategoryMo {
  late String name;
  late int count;

  CategoryMo.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    count = json['count'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['count'] = this.count;
    return data;
  }
}

写回答

3回答

CrazyCodeBoy

2023-03-08

需要用异步的方式来获取才行,可参考下这块课程源码的实现:
https://git.imooc.com/coding-487/fa-home/src/master/lib/page/home_page.dart#L112
0
0

CrazyCodeBoy

2023-01-03

对照下这块课程源码检查下你的代码实现修改下出入的地方:
https://git.imooc.com/coding-487
0
0

qq_稻草人_5

提问者

2023-01-02

没有网络报错,返回信息和API接口一直,就在转换HomeMo的地方报错

0
1
小苗晓雪
这返回的是一个 future 对象啊 , 你没加 async 和 await 啊兄弟~
2023-03-07
共1条回复

Flutter高级进阶实战-仿哔哩哔哩-掌握Flutter高阶技能

一次性掌握Flutter高阶技能+商业级复杂项目架构设计与开发方案

1722 学习 · 870 问题

查看课程