NoSuchMethodError:方法“ map”在null上调用。扑朔迷离

时间:2019-02-15 13:02:54

标签: android ios flutter

我正在尝试将响应传递给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';
  }
}

这是我的Imagelist类:

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。对我来说,响应正确显示,但是我不知道在解析显示此类错误的缩略图URL时。请检查我的代码并尝试为我提供帮助。

错误:

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 (模型));

2 个答案:

答案 0 :(得分:0)

请调试您的应用,并检查add_filter( 'woocommerce_order_get_items', 'filter_order_get_items', 10, 3 ); function filter_order_get_items( $items, $order, $types = 'line_item' ) { if(count($items) < 2) return $items; if( $types != 'line_item') return $items; $item_skus = $sorted_items = array(); // Loop through order line items foreach( $items as $items_id => $item ){ // Check items type: for versions before Woocommerce 3.3 if($item->is_type('line_item')){ $product = $item->get_product(); // $item_skus[$product->get_sku()] = $items_id; } } // Check items type: for versions before Woocommerce 3.3 (2) if( count($item_skus) == 0 ) return $items; // Sorting in ASC order based on SKUs; ksort($item_skus); // or use krsort() for DESC order // Loop through sorted $item_skus array foreach( $item_skus as $sku => $item_id ){ // Set items in the correct order $sorted_items[$item_id] = $items[$item_id]; } return $sorted_items; } 行中的images是否为null

答案 1 :(得分:0)

如果您到达这里,则意味着您遇到了同样的问题,尽管最初的问题已超过 2 年。您是否看过有关 Null 安全检查的问题?谷歌它并为我们报告是否解决了您的问题以及它是如何使用的。它可以帮助其他人前进。