颤振无法分配参数类型'List<dynamic>'

Posted

技术标签:

【中文标题】颤振无法分配参数类型\'List<dynamic>\'【英文标题】:flutter The argument type 'List<dynamic>' can't be assigned颤振无法分配参数类型'List<dynamic>' 【发布时间】:2021-09-13 13:42:44 【问题描述】:

大家好,我遇到了一个不知道如何解决的问题。

我有一个 Character 类:

import 'package:flutter/material.dart';

class Character 
  final String name;
  final String imagePath;
  final String description;
  final List<Color> colors;

  Character(
    required this.name,
    required this.imagePath,
    required this.description,
    required this.colors,
  );


List characters = [
  Character(
      name: 'Kevin',
      imagePath: 'assets/images/Kevin_minions.png',
      description:
          'Sir Kevin KBE (formerly known as Kevin) is one of the Minions and the protagonist in the film Minions. Kevin is a tall, two-eyed minion with sprout cut hair and is usually seen wearing his golf apparel. Kevin loves to make fun of and tease people or Minions, shown when he made fun of Jerry and teases him for being a coward. He loves playing golf and cricket. In the film Minions he is the leader of the trio in search of a new master. He truly cares about the well-being of the Minion tribe (which is dependent on them having a proper master).',
      colors: [Colors.orange.shade200, Colors.deepOrange.shade400]),
  Character(
      name: "Agnes",
      imagePath: "assets/images/Agnes_gru.png",
      description:
          "Agnes Gru it is one of Gru and Lucy's three adopted daughters, alongside her sisters Margo and Edith. She is the youngest child of the three sisters. She greatly adores unicorns, as shown on various occasions. Agnes is a little girl with dark brown eyes. Her long black hair is tied in an upwards ponytail with a red scrunchie. Most of the time, Agnes wears blue overalls over a yellow and brown striped t-shirt, and white sneakers with yellow socks. She also wears a white ballet outfit like Edith and Margo (at the ballet recital). For pajamas, Agnes wears a long blue nightshirt covered with teddy bears and polar bear slippers; her hair stays the same. On her birthday, Agnes is wearing a dress that resembles a princess riding a unicorn. The colors are similar to her regular outfit. She also wears a blue princess hat on her head.",
      colors: [Colors.pink.shade200, Colors.redAccent.shade400]),
];

也是一个角色信息或详细信息页面:

import 'package:flutter/material.dart';
import 'package:mimir_minions/models/character.dart';
import 'package:mimir_minions/styleguide.dart';

class CharacterDetailPage extends StatefulWidget 
  final Character character;

  const CharacterDetailPage(
    Key? key,
    required this.character,
  ) : super(key: key);

  @override
  _CharacterDetailPageState createState() => _CharacterDetailPageState();


class _CharacterDetailPageState extends State<CharacterDetailPage> 
  @override
  Widget build(BuildContext context) 
    final screenHeight = MediaQuery.of(context).size.height;

    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: [
          Hero(
            tag: "background_$widget.character.name",
            child: DecoratedBox(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(35),
                  topRight: Radius.circular(35),
                ),
                gradient: LinearGradient(
                  colors: widget.character.colors,
                  begin: Alignment.topRight,
                  end: Alignment.bottomLeft,
                ),
              ),
            ),
          ),
          SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.fromLTRB(16.0, 56.0, 0, 0),
                  child: IconButton(
                    icon: Icon(
                      Icons.close,
                    ),
                    iconSize: 48,
                    color: Colors.white.withOpacity(0.8),
                    onPressed: () 
                      Navigator.pop(context);
                    ,
                  ),
                ),
                Align(
                  alignment: Alignment.topRight,
                  child: Hero(
                    tag: "image_$widget.character.name",
                    child: Image.asset(
                      widget.character.imagePath,
                      height: screenHeight * 0.45,
                    ),
                  ),
                ),
                Padding(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 32.0, vertical: 8),
                  child: Hero(
                      tag: "name_$widget.character.name",
                      child: Material(
                          color: Colors.transparent,
                          child: Container(
                              child: Text(widget.character.name,
                                  style: AppTheme.heading)))),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(16.0, 0, 32.0, 24.0),
                  child: Text(widget.character.description,
                      style: AppTheme.subHeading),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  

还有一个角色小部件页面:

import 'package:flutter/material.dart';
import 'package:mimir_minions/models/character.dart';
import 'package:mimir_minions/pages/character_detail_page.dart';
import 'package:mimir_minions/styleguide.dart';

class CharacterWidget extends StatelessWidget 
  const CharacterWidget(Key? key, required this.character) : super(key: key);

  final Character character;

  @override
  Widget build(BuildContext context) 
    final screenHeight = MediaQuery.of(context).size.height;
    final screenWidth = MediaQuery.of(context).size.width;

    return InkWell(
      onTap: () 
        Navigator.push(
          context,
          PageRouteBuilder(
            transitionDuration: Duration(milliseconds: 350),
            pageBuilder: (context, _, __) => CharacterDetailPage(
              character: characters,
            ),
          ),
        );
      ,
      child: Stack(
        children: [
          Align(
            alignment: Alignment.bottomCenter,
            child: Hero(
              tag: "background_$characters[0].name",
              child: ClipPath(
                clipper: CharacterCardBackgroundClipper(),
                child: Container(
                  height: screenHeight * 0.6,
                  width: screenWidth * 0.9,
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: characters[0].colors,
                      begin: Alignment.topRight,
                      end: Alignment.bottomLeft,
                    ),
                  ),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment(0, -0.6),
            child: Hero(
              tag: "image_$characters[0].name",
              child: Image.asset(
                characters[0].imagePath,
                height: screenHeight * 0.55,
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 32, right: 8, bottom: 16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Hero(
                    tag: "name_$characters[0].name",
                    child: Material(
                        color: Colors.transparent,
                        child: Container(
                            child: Text(characters[0].name,
                                style: AppTheme.heading)))),
                Text('Tap to read more', style: AppTheme.subHeading),
              ],
            ),
          )
        ],
      ),
    );
  


class CharacterCardBackgroundClipper extends CustomClipper<Path> 
  @override
  Path getClip(Size size) 
    Path clippedPath = Path();
    double curveDistance = 40;

    clippedPath.moveTo(0, size.height * 0.4);
    clippedPath.lineTo(0, size.height - curveDistance);
    clippedPath.quadraticBezierTo(
        1, size.height - 1, 0 + curveDistance, size.height);
    clippedPath.lineTo(size.width - curveDistance, size.height);
    clippedPath.quadraticBezierTo(size.width + 1, size.height - 1, size.width,
        size.height - curveDistance);
    clippedPath.lineTo(size.width, 0 + curveDistance);
    clippedPath.quadraticBezierTo(size.width - 1, 0,
        size.width - curveDistance - 5, 0 + curveDistance / 3);
    clippedPath.lineTo(curveDistance, size.height * 0.29);
    clippedPath.quadraticBezierTo(
        1, (size.height * 0.30) + 10, 0, size.height * 0.4);
    return clippedPath;
  

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) 
    return true;
  

当我将字符用作character[0].name 之类的列表时,我没有问题。但是,当我想将其更改为 character 所以我的第一页上有几个字符小部件时,它给了我一个错误提示

无法将参数类型“列表”分配给参数 输入“字符”.dart(argument_type_not_assignable)

This is the link to my repo on Github

【问题讨论】:

【参考方案1】:

当 Dart 需要单个 Character 实例时,您尝试使用 List 时,错误很明显。

我认为您想要实现的是主/详细视图。 你应该有一个 CharacterList 小部件

class CharacterList extends StatelessWidget 
  final List<Character> characters;

  const CharacterList(Key? key, required this.characters) : super(key: key);

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      body: ListView.builder(
          itemCount: characters.length, itemBuilder: (context, index) => CharacterWidget(character: characters[index])),
    );
  

ListView 将成为您的主视图,并且在任何CharacterWidget 上,它们都应该将您带到详细视图,但您应该开始使用character 属性而不是characters 列表中的@987654326 @

 return InkWell(
      onTap: () 
        Navigator.push(
          context,
          PageRouteBuilder(
            transitionDuration: Duration(milliseconds: 350),
            pageBuilder: (context, _, __) => CharacterDetailPage(
              character: character,
            ),
          ),
        );
      ,
      child: Stack(
        children: [
          Align(
            alignment: Alignment.bottomCenter,
            child: Hero(
              tag: "background_$character.name",// instead of  "background_$characters[0].name"
              child: ClipPath(

【讨论】:

没问题,希望它能解决您的问题。如果是这种情况,请将答案标记为已接受或发布您自己的答案,如果它与我的距离太远,请将其标记为已接受,以便其他用户将其视为answered 非常感谢,这实际上对我不起作用,我意识到我应该使用 character 而不是 characters

以上是关于颤振无法分配参数类型'List<dynamic>'的主要内容,如果未能解决你的问题,请参考以下文章

颤振错误:参数类型“列表<动态>?”不能分配给参数类型“列表<动态>”

颤振列表错误参数类型'List'不能分配给参数类型'String'

颤振“参数类型不可分配”两种相同类型的错误

类型 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型在颤振应用程序中出现此错误

Flutter 错误:无法将参数类型“List<Future<Widget>>”分配给参数类型“List<Widget>”

错误:无法将参数类型“Null Function(DateTime, List<dynamic>)”分配给参数