类型'_BuiltList <data>'不是类型'List <data>'

时间:2019-02-11 06:39:30

标签: dart flutter

我收到类似以下的回复

{
    "status": 1,
    "msg": "Success",
    "data": [
        {
            "student_id": "1",
            "stud_name": "Aashr  ",
            "stud_profilepic": "http://default/default.png",
            "student_email": "a.su.com",
            "student_mobile": "9819",
            "course_name": "Busieurship",
            "class_code": "ISM-A",
            "year_name": "2020",
            "disciplineId": "1",
            "schoolId": "2",
            "minAvg": 30,
            "avg": 55
        },
        {
            "student_id": "2",
            "stud_name": "Aas ",
            "stud_profilepic": "http:lt/default.png",
            "student_email": "aasl.com",
            "student_mobile": "975",
            "course_name": "Businurship",
            "class_code": "ISM-B",
            "year_name": "2020",
            "disciplineId": "1",
            "schoolId": "2",
            "minAvg": 30,
            "avg": 19
        }....

我正在关注此示例

https://github.com/ReactiveX/rxdart/tree/master/example/flutter/github_search

当我将json转换为Dart

class StudentModel {
  int status;
  String msg;
  List<Data> data;

  StudentModel({this.status, this.msg, this.data});

  StudentModel.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    msg = json['msg'];
    if (json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v) {
        data.add(new Data.fromJson(v));
      });
    }
  }

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

class Data {
  String studentId;
  String studName;
  String studProfilepic;
  String studentEmail;
  String studentMobile;
  String courseName;
  String classCode;
  String yearName;
  String disciplineId;
  String schoolId;
  int minAvg;
  int avg;

  Data(
      {this.studentId,
        this.studName,
        this.studProfilepic,
        this.studentEmail,
        this.studentMobile,
        this.courseName,
        this.classCode,
        this.yearName,
        this.disciplineId,
        this.schoolId,
        this.minAvg,
        this.avg});

  Data.fromJson(Map<String, dynamic> json) {
    studentId = json['student_id'];
    studName = json['stud_name'];
    studProfilepic = json['stud_profilepic'];
    studentEmail = json['student_email'];
    studentMobile = json['student_mobile'];
    courseName = json['course_name'];
    classCode = json['class_code'];
    yearName = json['year_name'];
    disciplineId = json['disciplineId'];
    schoolId = json['schoolId'];
    minAvg = json['minAvg'];
    avg = json['avg'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['student_id'] = this.studentId;
    data['stud_name'] = this.studName;
    data['stud_profilepic'] = this.studProfilepic;
    data['student_email'] = this.studentEmail;
    data['student_mobile'] = this.studentMobile;
    data['course_name'] = this.courseName;
    data['class_code'] = this.classCode;
    data['year_name'] = this.yearName;
    data['disciplineId'] = this.disciplineId;
    data['schoolId'] = this.schoolId;
    data['minAvg'] = this.minAvg;
    data['avg'] = this.avg;
    return data;
  }
}

我在我的代码中使用了所有功能,但假设我使用内置值而不是将json转换为Dart

library student_model;

import 'dart:convert';

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:search/models/serializers.dart';

part 'student_model.g.dart';

abstract class StudentModel implements Built<StudentModel, StudentModelBuilder> {
  StudentModel._();

  factory StudentModel([updates(StudentModelBuilder b)]) = _$StudentModel;

  @nullable
  @BuiltValueField(wireName: 'status')
  int get status;
  @nullable
  @BuiltValueField(wireName: 'msg')
  String get msg;
  @nullable
  @BuiltValueField(wireName: 'data')
  BuiltList<Data> get data;

  String toJson() {
    return json.encode(serializers.serializeWith(StudentModel.serializer, this));
  }

  static StudentModel fromJson(String jsonString) {
    return serializers.deserializeWith(
        StudentModel.serializer, json.decode(jsonString));
  }

  static Serializer<StudentModel> get serializer => _$studentModelSerializer;
}

abstract class Data implements Built<Data, DataBuilder> {
  Data._();

  factory Data([updates(DataBuilder b)]) = _$Data;

  @nullable
  @BuiltValueField(wireName: 'student_id')
  String get studentId;
  @nullable
  @BuiltValueField(wireName: 'stud_name')
  String get studName;
  @nullable
  @BuiltValueField(wireName: 'stud_profilepic')
  String get studProfilepic;
  @nullable
  @BuiltValueField(wireName: 'student_email')
  String get studentEmail;
  @nullable
  @BuiltValueField(wireName: 'student_mobile')
  String get studentMobile;
  @nullable
  @BuiltValueField(wireName: 'course_name')
  String get courseName;
  @nullable
  @BuiltValueField(wireName: 'class_code')
  String get classCode;
  @nullable
  @BuiltValueField(wireName: 'year_name')
  String get yearName;
  @nullable
  @BuiltValueField(wireName: 'disciplineId')
  String get disciplineId;
  @nullable
  @BuiltValueField(wireName: 'schoolId')
  String get schoolId;
  @nullable
  @BuiltValueField(wireName: 'minAvg')
  int get minAvg;
  @nullable
  @BuiltValueField(wireName: 'avg')
  int get avg;

  String toJson() {
    return json.encode(serializers.serializeWith(Data.serializer, this));
  }

  static Data fromJson(String jsonString) {
    return serializers.deserializeWith(
        Data.serializer, json.decode(jsonString));
  }

  static Serializer<Data> get serializer => _$dataSerializer;
}

我的代码中出现以下错误

 The following assertion was thrown building StreamBuilder<SearchState>(dirty, state:
I/flutter ( 3684): _StreamBuilderBaseState<SearchState, AsyncSnapshot<SearchState>>#a0ef6):
I/flutter ( 3684): type '_BuiltList<Data>' is not a subtype of type 'List<Data>'
I/flutter ( 3684): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 3684): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 3684): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 3684):   https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 3684): When the exception was thrown, this was the stack:
I/flutter ( 3684): #0      SearchScreenState.build.<anonymous closure> (package:search/screens/search_widget.dart:88:54)
I/flutter ( 3684): #1      StreamBuilder.build (package:flutter/src/widgets/async.dart:423:74)
I/flutter ( 3684): #2      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:125:48)
I/flutter ( 3684): #3      StatefulElement.build (package:flutter/src/widgets/framework.dart:3809:27)
I/flutter ( 3684): #4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3721:15)
I/flutter ( 3684): #5      Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3684): #6      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2286:33)
I/flutter ( 3684): #7      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:676:20)
I/flutter ( 3684): #8      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5)
I/flutter ( 3684): #9      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 3684): #10     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter ( 3684): #11     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter ( 3684): #12     _invoke (dart:ui/hooks.dart:154:13)
I/flutter ( 3684): #13     _drawFrame (dart:ui/hooks.dart:143:3)

这是我的请求代码

Future<StudentModel> fetchStudents(String disciplineId, String schoolId,
      String year_id, String keyword) async {
    final response = await http.post(GET_STUDENTS, body: {
      "disciplineId": disciplineId,
      "schoolId": schoolId,
      "year_id": year_id,
      "keyword": keyword
    });

    print("response2 is ${response.body.toString()}");
    StudentModel studentModel = standardSerializers.deserializeWith(StudentModel.serializer, jsonDecode(response.body));
    print("response is ${studentModel.toString()}");
    return studentModel;

  }

我还尝试将standardSerializers替换为普通的serializers,如下所示,但它会出错

 StudentModel studentModel = serializers.deserializeWith(StudentModel.serializer, jsonDecode(response.body));

为什么这是内置值问题,而不是普通的json到Dart转换的问题?

1 个答案:

答案 0 :(得分:1)

BuiltList<Data>不是{strong> a List<Data>的地方传递BuiltList,则需要使用List

例如在此行

.toList()

使用I/flutter ( 3684): #0 SearchScreenState.build.<anonymous closure> (package:search/screens/search_widget.dart:88:54) 代替data

或更改接收器,以使其接受data.toList()代替BuiltList<Data>