gpt4 book ai didi

android - NoSuchMethodError : The method 'map' was called on null. flutter

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

我正在尝试将响应传递给 arraylist 的网格值。

// To parse this JSON data, do
//
// final imageModel = imageModelFromJson(jsonString);

import 'dart:convert';

ImageModel imageModelFromJson(String str) {
final jsonData = json.decode(str);
return ImageModel.fromJson(jsonData);
}

String imageModelToJson(ImageModel data) {
final dyn = data.toJson();
return json.encode(dyn);
}

class ImageModel {
int status;
List<Datum> data;
int totalRecord;

ImageModel({
this.status,
this.data,
this.totalRecord,
});


factory ImageModel.fromJson(Map<String, dynamic> json) => new ImageModel(
status: json["status"],
data: new List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
totalRecord: json["totalRecord"],
);

Map<String, dynamic> toJson() => {
"status": status,
"data": new List<dynamic>.from(data.map((x) => x.toJson())),
"totalRecord": totalRecord,
};

@override
String toString() {
// TODO: implement toString
return '$status $data $totalRecord';
}
}

class Datum {
String id;
String name;
String thumbnailUrl;
int plays;
String categories;
String categoriesIds;
String dataUrl;
int duration;
String createdAt;
int status;
int statusc;
int mediaType;

Datum({
this.id,
this.name,
this.thumbnailUrl,
this.plays,
this.categories,
this.categoriesIds,
this.dataUrl,
this.duration,
this.createdAt,
this.status,
this.statusc,
this.mediaType,
});

factory Datum.fromJson(Map<String, dynamic> json) => new Datum(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
thumbnailUrl: json["thumbnailUrl"] == null ? null : json["thumbnailUrl"],
plays: json["plays"] == null ? null : json["plays"],
categories: json["categories"] == null ? null : json["categories"],
categoriesIds: json["categoriesIds"] == null ? null : json["categoriesIds"],
dataUrl: json["dataUrl"] == null ? null : json["dataUrl"],
duration: json["duration"] == null ? null : json["duration"],
createdAt: json["createdAt"] == null ? null : json["createdAt"],
status: json["status"] == null ? null : json["status"],
statusc: json["statusc"] == null ? null : json["statusc"],
mediaType: json["mediaType"] == null ? null : json["mediaType"],
);

Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"thumbnailUrl": thumbnailUrl == null ? null : thumbnailUrl,
"plays": plays == null ? null : plays,
"categories": categories == null ? null : categories,
"categoriesIds": categoriesIds == null ? null : categoriesIds,
"dataUrl": dataUrl == null ? null : dataUrl,
"duration": duration == null ? null : duration,
"createdAt": createdAt == null ? null : createdAt,
"status": status == null ? null : status,
"statusc": statusc == null ? null : statusc,
"mediaType": mediaType == null ? null : mediaType,
};

@override
String toString() {
// TODO: implement toString
return '$id $name $thumbnailUrl $plays $categories $categoriesIds $dataUrl $duration';
}
}

这是我的图像列表类:

import 'package:flutter/material.dart';
import 'package:ottdemo_flutter/image_model.dart';


class ImageList extends StatelessWidget {
final List<ImageModel>images;
ImageModel data = new ImageModel();
ImageList(this.images);

Widget build(context){
return GridView.builder(
itemCount:images.length,
itemBuilder: (context,int index){
return buildImage(images[index]);
}, gridDelegate: null,
);
}

Widget buildImage(ImageModel image) {
return Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
),
padding: EdgeInsets.all(20.0),
margin: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Padding(
//child:Image.network(image.data.toList(growable:true)),
padding: EdgeInsets.only(
bottom:10.0
),
),
// Text(image.thumbnailUrl),
],
),
);
}

}

这是我从新类调用的主要方法。

if(res.statusCode == 200){
print(mediaResponse);
if(mediaResponse.status == 1){
final imageModel =json.decode(res.body);
var imageData = imageModel['data'] as List;
for(var model in imageData){
setState(() {
images.add(new ImageModel.fromJson(model));
});
}
print("List Size: ${images.length}");
}

现在我收到一个错误 NoSuchMethodError。对我来说,响应显示正确,但我不知道在解析显示类似错误的缩略图网址时。请检查我的代码并尝试帮助我。

错误:

Tried calling: map(Closure: (dynamic) => Datum)
E/flutter (14235): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
E/flutter (14235): #1 new ImageModel.fromJson (package:ottdemo_flutter/image_model.dart:31:45)
E/flutter (14235): #2 _MainActivityGridState.fetchImage.<anonymous closure> (package:ottdemo_flutter/mainactivity.dart:59:26)

此行显示错误。

images.add(新ImageModel.fromJson(模型));

最佳答案

请调试您的应用并检查 images.add(new ImageModel.fromJson(model)); 行中的 images 是否为 null

关于android - NoSuchMethodError : The method 'map' was called on null. flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54709966/

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