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

Posted

技术标签:

【中文标题】颤振列表错误参数类型\'List\'不能分配给参数类型\'String\'【英文标题】:flutter list error The argument type 'List' can't be assigned to parameter type 'String'颤振列表错误参数类型'List'不能分配给参数类型'String' 【发布时间】:2019-12-29 14:40:02 【问题描述】:

我收到一条错误消息;

参数类型“列表”不能分配给参数类型“字符串”

关于 widget.storePageImage、widget.storePageItemName 和 widget.storePageItemPrice。

我怎样才能做到这一点?

这就是我想要实现的; 一旦点击主页中的商店(https://imgur.com/a/0YiMUHj),它应该将其定向到该单独的商店页面(https://imgur.com/a/KbsIepO)。

import 'package:flutter/material.dart';
import 'package:filename/src/homescreen.dart';
import 'package:filename/data/Store_list.dart';

class StoresPage extends StatefulWidget 
String storeName;
String storeDeliveryTime;
String deliveryCharges;
List<String> storePageImage;
List<String> storePageItemName;
List<String> storePageItemPrice;

StoresPage(
this.storeName, this.storeDeliveryTime, this.deliveryCharges, 
this.storePageImage, this.storePageItemName, this.storePageItemPrice
);

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


class _StoresPageState extends State<StoresPage> 

@override
Widget build(BuildContext context) 
return Scaffold(
  body: ListView(
    children: <Widget>[
      Container(
        padding: EdgeInsets.only(right: 15.0, top: 9.0, left: 10.0),
        margin: EdgeInsets.only(bottom: 10.0),
        child: Row(
          children: <Widget>[
            Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 20.0, right: 
                  5.0),
                  child: GestureDetector(
                      onTap: ()Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => 
                        HomeScreen()),
                      );,
                      child: Icon(Icons.navigate_before, color: 
                      Colors.black87,)),
                ),
              ],
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text("You're now", style: TextStyle(color: 
                Colors.grey),),
                Row(
                  children: <Widget>[
                    Text("Shopping at", style: TextStyle(fontSize: 
                    16.0, fontWeight: FontWeight.bold),),
                    SizedBox(width: 5.0,),
                    Text(widget.storeName, style: 
                    TextStyle(fontSize: 16.0, fontWeight: 
                    FontWeight.bold),),
                  ],
                ),
              ],
            ),
            Spacer(),
            Container(
              height: 60,
              width: 60,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage('assets/images/profilepc.png'),
                ),
              ),
            ),
          ],
        ),
      ),
      Container(
        margin: EdgeInsets.only(bottom: 25.0),
        height: 35.0,
        width: 380.0,
        child: Padding(
          padding: const EdgeInsets.only(left: 15.0, right: 15.0),
          child: TextField(
            style: TextStyle(fontSize: 15.0),
            decoration: InputDecoration(
              filled: true,
              fillColor: Color(0xFFEEEEEE),
              contentPadding: EdgeInsets.symmetric(horizontal: 5.0, 
              vertical: 5.0),
              hintText: "Search  $widget.storeName",
              prefixIcon: Icon(Icons.search, color: Colors.black,),
              border: OutlineInputBorder(
                  borderRadius: 
              BorderRadius.all(Radius.circular(30.0))),
            ),
          ),
        ),
      ),
      Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(widget.storeName, style: TextStyle(fontSize: 
              25.0),)
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Icon(Icons.motorcycle),
              SizedBox(width: 7.0),
              Text(widget.deliveryCharges),
              SizedBox(width: 20.0,),
              Icon(Icons.update),
              SizedBox(width: 5.0),
              Text(widget.storeDeliveryTime),
            ],
          ),
          Padding(
            padding: const EdgeInsets.only(top: 10.0, bottom: 1.0),
            child: Divider(height: 20.0, color: Colors.grey,),
          ),
        ],
      ),

      SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(left: 10.0, top: 7.0),
                    child: Container(
                      height: 100,
                      width: 100,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(30),
                        image: DecorationImage(
                          fit: BoxFit.fill,
                          image: AssetImage(widget.storePageImage),
                        ),
                      ),
                    ),
                  ),
                Text(widget.storePageItemName),
                Spacer(),
                Padding(padding: const EdgeInsets.only(top: 52.0, 
                right: 10.0),
                 child: Column(children: <Widget>[
                  Text(widget.storePageItemPrice),
                ],),
                ),
              ],
            ),
          ],
        ),
      ),

    ],
  ),
);


数据文件

class Stores 
String storeName;
String storeImage;
String storeDeliveryTime;
String deliveryCharges;
List<String> storePageImage;
List<String> storePageItemName;
List<String> storePageItemPrice;

Stores.list(this.storeName, this.storeDeliveryTime
this.storeImage, this.deliveryCharges, this.storePageImage,
this.storePageItemName, this.storePageItemPrice);


List<Stores> storesList = [
Stores.list(
  storeName: "Store 1",
  storeImage: "assets/images/store1front.jpg",
  storeDeliveryTime: "25 min",
  deliveryCharges: "£3.90",
  storePageImage: ["assets/images/1.png","assets/images/2.png", 
  "assets/images/3.png","assets/images/4.png"],
  storePageItemName: ["Black TSHIRT","Khaki T-SHIRT","Grey Knit 
  Notch Neck","Teal Polo Shirt" ],
  storePageItemPrice: ["£17.99","17.99","17.99","17.99"] 
),

Stores.list(
storeName: "STORE2",
storeImage: "assets/images/STORE2front.jpg",
storeDeliveryTime: "25 min",
deliveryCharges: "£2.90",
storePageImage: 
  ["assets/images/store2_1.png","assets/images/store2_2.png", 
  "assets/images/store2_3.png","assets/images/store2_4.png"],
storePageItemName: ["WHITE TSHIRT","orange T-SHIRT","Grey 
                   shirt", "mint tea Polo Shirt" ],
storePageItemPrice: ["£18.99","10.99","16.99","13.99"]),];

【问题讨论】:

检查您的控制台日志,它将显示错误所在的确切代码行。 我在问题中提到了“关于widget.storePageImage、widget.storePageItemName 和widget.storePageItemPrice。” 我正在尝试复制该问题,但似乎我在您的代码中遗漏了一些重要部分。你能提供a minimal, complete and verifiable example吗? 【参考方案1】:

您收到错误The argument type 'List' can't be assigned to parameter type 'String' 的原因是该方法需要一个字符串,但正在使用一个列表。

日志指出导致问题的参数:widget.storePageImagewidget.storePageItemNamewidget.storePageItemPrice。此处使用这些参数。

SingleChildScrollView(
  child: Column(
     children: <Widget>[
       Row(
         children: <Widget>[
           Padding(
             padding: EdgeInsets.only(left: 10.0, top: 7.0),
             child: Container(
               height: 100,
               width: 100,
               decoration: BoxDecoration(
                 borderRadius: BorderRadius.circular(30),
                 image: DecorationImage(
                   fit: BoxFit.fill,
                   image: AssetImage(widget.storePageImage),
                 ),
               ),
             ),
           ),
           Text(widget.storePageItemName),
           Spacer(),
           Padding(padding: const EdgeInsets.only(top: 52.0, 
             right: 10.0),
             child: Column(children: <Widget>[
               Text(widget.storePageItemPrice),
             ],),
          ),
        ],
      ),
    ],
  ),
),

据我所知,您似乎正在尝试创建一个显示商店页面项目及其价格的小部件列表。您可以在这里做的是使用 ListView.builder() 为商店页面项目创建一个小部件列表。

ListView.builder(
  padding: const EdgeInsets.all(8),
  // assuming that storePageImage, storePageItemName, storePageItemPrice
  // has the same length. Otherwise, add safety checks for index OutOfBound errors
  itemCount: widget.storePageItemName.length,
  itemBuilder: (BuildContext context, int index) 
    return Row(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(left: 10.0, top: 7.0),
          child: Container(
            height: 100,
            width: 100,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(30),
              image: DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage(widget.storePageImage[index]),
              ),
            ),
          ),
        ),
        Text(widget.storePageItemName[index]),
        Spacer(),
        Padding(
          padding: const EdgeInsets.only(top: 52.0, right: 10.0),
          child: Column(
            children: <Widget>[
              Text(widget.storePageItemPrice[index]),
            ],
          ),
        ),
      ],
    );
  ,
)

【讨论】:

以上是关于颤振列表错误参数类型'List'不能分配给参数类型'String'的主要内容,如果未能解决你的问题,请参考以下文章

颤振错误:参数类型“列表<动态>?”无法分配给参数类型'List<Movie>' [关闭]

参数类型“Widget Function(Categoria)”不能分配给参数类型“dynamic Function(Child)”。 (型号)颤振

参数类型“对象?”不能分配给参数类型 List

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

参数类型“Future<>”不能分配给参数类型“”

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