【问题标题】:type 'List<String>' is not a subtype of type 'List<Map<dynamic, dynamic>>'类型“List<String>”不是类型“List<Map<dynamic, dynamic>>”的子类型
【发布时间】:2021-12-29 16:33:56
【问题描述】:

我正在构建这个应用程序,我想显示演员及其姓名的图像列表视图。所以我制作了一个 Movieclass,它包含电影的标题(字符串)、横幅路径(字符串)、描述(字符串)、id(int - 用于英雄动画)和演员阵容,这是一张地图。该地图包含图片的路径和演员的姓名。现在我想在我的小部件中使用它,但在尝试这样做之后,我去了上面的箭头。这是模型的代码:

import 'package:flutter/material.dart';

List<MovieModel> get models =>
    movies.map((movie) => MovieModel.fromJson(movie)).toList();

List<Widget> get widgets =>
    models.map((model) => MovieWidget(model: model)).toList();

class MovieModel {
  MovieModel({
    required this.title,
    required this.cast,
    required this.bannerPath,
    required this.description,
    required this.id,
  });

  factory MovieModel.fromJson(Map<String, dynamic> json) {
    return MovieModel(
      title: json['title'],
      cast: json['cast'],
      bannerPath: json['bannerPath'],
      description: json['description'],
      id: json['id'],
    );
  }

  String title;
  String bannerPath;
  String description;
  List<Map> cast;
  int id;
}

这是我声明的电影变量,其中包含我想在应用程序中显示的所有信息...

var movies = [
  {
    'title': '21',
    'bannerPath': 'assets/images/21_movie.jpeg',
    'id': 1,
    'description':
        '"21" is the fact-based story about six MIT students who were trained to become experts in card counting and subsequently took Vegas casinos for millions in winnings.',
    'cast': [
      {
        'name': 'Kate Bosworth',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Jim Slurgess',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Kevin Spacey',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
    ],
  },
  {
    'title': 'Endless Love',
    'bannerPath': 'assets/images/endless_love_movie.jpeg',
    'id': 2,
    'description':
        'The story of a privileged girl and a charismatic boy whose instant desire sparks a love affair made only more reckless by parents trying to keep them apart',
    'cast': [
      {
        'name': 'Alex Pettyfer',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Garbielle Wilde',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'bruce Greenwood',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
    ],
  },
  {
    'title': 'Kevin allein zuhaus',
    'bannerPath': 'assets/images/kevin_allein_zuhaus_movie.jpeg',
    'id': 3,
    'description':
        'Dieses Jahr fliegt die gesamte McAllister-Familie nach Frankreich, um dort gemeinsam Weihnachten zu feiern. Leider haben sie bei ihrer hektischen Abreise ihren achtjährigen Sprößling Kevin zu Hause vergessen. Dieser genießt natürlich seine neugewonnene Freiheit.',
    'cast': [
      {
        'name': 'Macaula Culkin',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Joe Pesci',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'John Heard',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
    ],
  },
  {
    'title': 'Little Woman',
    'bannerPath': 'assets/images/little_woman_movie.jpeg',
    'id': 4,
    'description':
        'rThe story follows the lives of the four March sisters—Meg, Jo, Beth, and Amy—and details their passage from childhood to womanhood. Loosely based on the lives of the author and her three sisters, it is classified as an autobiographical or semi-autobiographical novel.',
    'cast': [
      {
        'name': 'Saorse ROnan',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Timothée Chalmet',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Florence Pugh',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
    ],
  },
  {
    'title': 'Liebe braucht keine Ferien',
    'bannerPath': 'assets/images/love_movie.jpeg',
    'id': 5,
    'description':
        'Amanda ist enttäuscht von der Liebe und will nichts wie weg aus Los Angeles. Genauso geht es Iris in England. Über das Internet vereinbaren die beiden Frauen einen Häusertausch. In England trifft Amanda schon bald auf Iris attraktiven Bruder, und Iris lernt einen Arbeitskollegen von Amanda kennen.',
    'cast': [
      {
        'name': 'Jude Law',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Cameron Diaz',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Kate Winslet',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
    ],
  },
  {
    'title': 'Marvel Movies',
    'bannerPath': 'assets/images/marvel_movie.jpeg',
    'id': 6,
    'description':
        'The Marvel Cinematic Universe (MCU) is an American media franchise and shared universe centered on a series of superhero films produced by Marvel Studios.',
    'cast': [
      {
        'name': 'Chris Evans',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Robert Downey Jr.',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Chris Hemsworth',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Scarlett Johansson',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
    ],
  },
  {
    'title': 'Oceans 12',
    'bannerPath': 'assets/images/oceans_12_movie.jpeg',
    'id': 7,
    'description':
        'Set three years after "Ocean\'s 11," this sequel shows us Danny Ocean gathering up his complete gang of con artists and thieves from the first film in New York City before they all jet off to Amsterdam, Rome, and Paris to pull off three seperate heists.',
    'cast': [
      {
        'name': 'Geroge Cloney',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Julia Roberts',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Catherine Zeta-James',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Brad Pitt',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
    ],
  },
  {
    'title': 'A star is born',
    'bannerPath': 'assets/images/star_movie.jpeg',
    'id': 8,
    'description':
        'Der in die Jahre gekommene Rockstar Jackson Maine entdeckt die talentierte Sängerin Ally und verliebt sich in sie. Ally hätte ihren großen Traum von einer Musikkarriere fast aufgegeben. Jackson fördert sie und nimmt sie schließlich sogar mit auf seine Tournee.',
    'cast': [
      'Lady Gaga',
      'Bradley Cooper',
      'Sam Elliot',
    ]
  },
  {
    'title': 'Frühstück bei Tiffany',
    'bannerPath': 'assets/images/tiffany_movie.jpeg',
    'id': 10,
    'description':
        'Holly Golightly sucht einen reichen Ehemann, hat gutbetuchte Freunde, eine namenlose Katze und einen mittellosen Begleiter, der ihr immer aus der Patsche hilft. Ihr Traum: eine Wohnung so schön wie bei Tiffanys. Doch dann fordert der Exmann alte Rechte ein, die Polizei stellt unbequeme Fragen, und der Heiratskandidat aus Brasilien verrschwindet.',
    'cast': [
      {
        'name': 'Audrey Hepburn',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'George Peppard',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
      {
        'name': 'Patricia Neal',
        'image': 'assets/images/actors/kate-bosworth--6282909-.jpg'
      },
    ],
  },
];

我在寻找解决方案,但到目前为止我找不到...我刚开始使用颤振,现在我几乎没有选择,提前感谢您的帮助:)

【问题讨论】:

  • “一个明星的诞生”的演员阵容与其他电影不同。
  • 好的,所以我没看到...我不再收到错误,但是当我尝试访问详细信息屏幕时,我收到了这个错误+屏幕冻结:drive.google.com/file/d/13z7y4GGh5SUWf4D4mihq9D_13RNHKM6p/…跨度>
  • 我脑子里没有答案,但这是一个完全不同的问题。也许考虑再发一篇文章,详细介绍这个新问题。

标签: list flutter dictionary dart oop


【解决方案1】:

我认为这只是类型问题。如果你声明这样的电影:List&lt;Map&lt;String, dynamic&gt;&gt; movies,并创建这样的类:List&lt;MovieModel&gt; mov = movies.map((e) =&gt; MovieModel.fromJson(e)).toList(); },它应该可以工作。

【讨论】:

    猜你喜欢
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2020-07-03
    • 2019-12-05
    • 2023-03-11
    • 2019-04-03
    • 2021-09-12
    相关资源
    最近更新 更多