gpt4 book ai didi

dart - 类型 '_BuiltList' 不是类型 'List' 的子类型

转载 作者:IT王子 更新时间:2023-10-29 06:58:29 26 4
gpt4 key购买 nike

我有类似下面的回复

{
"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;

}

我也尝试用正常的 serializers 替换 standardSerializers 如下所示,但它给出了错误

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

为什么这是构建值的问题而不是普通的 json 到 Dart 转换的问题?

最佳答案

BuiltList<Data> 不是 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>而不是 List<Data>

关于dart - 类型 '_BuiltList<Data>' 不是类型 'List<Data>' 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54625129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com