Flutter - 需要 2 个必需参数,但找到 0 个

Posted

技术标签:

【中文标题】Flutter - 需要 2 个必需参数,但找到 0 个【英文标题】:Flutter - 2 required argument(s) expected, but 0 found 【发布时间】:2019-02-04 15:45:59 【问题描述】:

我正在创建一个双 listView 应用程序,到目前为止运行良好,但我遇到了一个似乎无法修复的问题。

我收到错误消息[dart] 2 required argument(s) expected, but 0 found.

在我看来,这应该很容易解决,我认为我只需要改变......

body: _cryptoWidget(),

到...

body: _cryptoWidget(currency, color),

这不起作用,我仍然收到上述错误。

这是我用来激发错误的代码;

import 'package:flutter/material.dart';
import 'background.dart';
import 'package:flutter/foundation.dart';
import 'package:cryptick_nice_ui/data/crypto_data.dart';
import 'package:cryptick_nice_ui/modules/crypto_presenter.dart';

class HomePage extends StatefulWidget 
  @override
  _HomePageState createState() => new _HomePageState();


class _HomePageState extends State<HomePage> implements CryptoListViewContract 
  CryptoListPresenter _presenter;
  List<Crypto> _currencies;
  bool _isLoading;
  final List<MaterialColor> _colors = [Colors.blue, Colors.indigo, Colors.red];

    List<String> items = [
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4",
    "Item 5",
    "Item 6",
    "Item 7",
    "Item 8"
  ];

  _HomePageState() 
    _presenter = new CryptoListPresenter(this);
  

  @override
  void initState() 
    super.initState();
    _isLoading = true;
    _presenter.loadCurrencies();
  


  @override
  Widget build(BuildContext context) 

  return new Container(
      decoration: new BoxDecoration(
      color: const Color(0xFF273A48),
    ),
    child: new Stack(
      children: <Widget>[
        new Scaffold(
        appBar: new AppBar(
        title: new Text("cryp"),
        elevation: 0.0,
        backgroundColor: Colors.transparent,
      ),
      backgroundColor: Colors.transparent,
          body: _cryptoWidget(),
          )
        ],
      ),
    );
  

  Widget _cryptoWidget(Crypto currency, MaterialColor color) 
    final _width = MediaQuery.of(context).size.width;
    final _height = MediaQuery.of(context).size.height;
    final headerList = new ListView.builder(
      itemBuilder: (context, index) 
        EdgeInsets padding = index == 0?const EdgeInsets.only(
            left: 20.0, right: 10.0, top: 4.0, bottom: 30.0):const EdgeInsets.only(
            left: 10.0, right: 10.0, top: 4.0, bottom: 30.0);
            _gethtmlUI();
        return new Padding(
          padding: padding,
        );
      ,
      scrollDirection: Axis.horizontal,
      itemCount: items.length,
    );
    new Scaffold(
      appBar: new AppBar(
        title: new Text('hello'),
        elevation: 0.0,
        backgroundColor: Colors.transparent,
      ),
      backgroundColor: Colors.transparent,
      body: new Container(
        child: new Stack(
          children: <Widget>[
            new Padding(
              padding: new EdgeInsets.only(top: 10.0),
              child: new Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  new Align(
                    alignment: Alignment.centerLeft,
                    child: new Padding(
                        padding: new EdgeInsets.only(left: 8.0),
                        child: new Text(
                          'Your Feed',
                          style: new TextStyle(color: Colors.white70),
                        )),
                  ),
                  new Container(
                      height: 300.0, width: _width, child: headerList),
                  new Expanded(child:
                  ListView.builder(
                    itemBuilder: 
                    (context, index) 
                    _getCryptoUI(currency, color);
                  
                  )
                )
                ],
              ),
            ),
          ],
        ),
      ),
    );

      return new Container(
      decoration: new BoxDecoration(
        color: const Color(0xFF273A48),
      ),
      child: new Stack(
        children: <Widget>[
          new CustomPaint(
            size: new Size(_width, _height),
            painter: new Background(),
          ),
        ],
      ),
    );


  

  Container _getHtmlUI() 
    return new Container(
              decoration: new BoxDecoration(
                borderRadius: new BorderRadius.circular(10.0),
                color: Colors.lightGreen,
                boxShadow: [
                  new BoxShadow(
                      color: Colors.black.withAlpha(70),
                      offset: const Offset(3.0, 10.0),
                      blurRadius: 15.0)
                ],
                image: new DecorationImage(
                  image: new ExactAssetImage(
                      'assets/img_0.jpg'),
                  fit: BoxFit.fitHeight,
                ),
              ),
//                                    height: 200.0,
              width: 200.0,
              child: new Stack(
                children: <Widget>[
                  new Align(
                    alignment: Alignment.bottomCenter,
                    child: new Container(
                        decoration: new BoxDecoration(
                            color: const Color(0xFF273A48),
                            borderRadius: new BorderRadius.only(
                                bottomLeft: new Radius.circular(10.0),
                                bottomRight: new Radius.circular(10.0))),
                        height: 30.0,
                        child: new Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            new Text(
                              'hello',
                              style: new TextStyle(color: Colors.white),
                            )
                          ],
                        )),
                  )
                ],
              ),
            );
  

  ListTile _getCryptoUI(Crypto currency, MaterialColor color) 
    return new ListTile(
                      title: new Column(
                        children: <Widget>[
                          new Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              new Container(
                                height: 72.0,
                                width: 72.0,
                                decoration: new BoxDecoration(
                                    color: Colors.lightGreen,
                                    boxShadow: [
                                      new BoxShadow(
                                          color:
                                          Colors.black.withAlpha(70),
                                          offset: const Offset(2.0, 2.0),
                                          blurRadius: 2.0)
                                    ],
                                    borderRadius: new BorderRadius.all(
                                        new Radius.circular(12.0)),
                                    image: new DecorationImage(
                                      image: new ExactAssetImage(
                                        "cryptoiconsBlack/"+currency.symbol.toLowerCase()+"@2x.png",
                                      ),
                                      fit: BoxFit.cover,
                                    )),
                              ),
                              new SizedBox(
                                width: 8.0,
                              ),
                              new Expanded(
                                  child: new Column(
                                    mainAxisAlignment:
                                    MainAxisAlignment.start,
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: <Widget>[
                                      new Text(
                                        currency.name,
                                        style: new TextStyle(
                                            fontSize: 14.0,
                                            color: Colors.black87,
                                            fontWeight: FontWeight.bold),
                                      ),
                                      _getSubtitleText(currency.price_usd, currency.percent_change_1h),
                                    ],
                                  )),
                              new Icon(
                                Icons.shopping_cart,
                                color: const Color(0xFF273A48),
                              )
                            ],
                          ),
                          new Divider(),
                        ],
                      ),
                    );
  


  Widget _getSubtitleText(String priceUSD, String percentageChange) 
    TextSpan priceTextWidget = new TextSpan(
        text: "\$$priceUSD\n", style: new TextStyle(color: Colors.black));
    String percentageChangeText = "1 hour: $percentageChange%";
    TextSpan percentageChangeTextWidget;

    if (double.parse(percentageChange) > 0) 
      percentageChangeTextWidget = new TextSpan(
          text: percentageChangeText,
          style: new TextStyle(color: Colors.green));
     else 
      percentageChangeTextWidget = new TextSpan(
          text: percentageChangeText, style: new TextStyle(color: Colors.red));
    

    return new RichText(
        text: new TextSpan(
            children: [priceTextWidget, percentageChangeTextWidget]));
  

  @override
  void onLoadCryptoComplete(List<Crypto> items) 
    // TODO: implement onLoadCryptoComplete

    setState(() 
      _currencies = items;
      _isLoading = false;
    );
  

  @override
  void onLoadCryptoError() 
    // TODO: implement onLoadCryptoError
  


有关我在此应用中使用的其他文件(例如 crypto_data.dart)的更多信息,请参阅 this GitHub repo 我用于对我的代码的某些部分进行建模。

【问题讨论】:

错误行号? body: _cryptoWidget(_currencies[0], _colors[0]),应该可以解决问题 @diegoveloper 这解决了问题,现在我可以加载应用程序,但现在除了我的自定义背景之外,页面上实际上没有加载任何内容。你能提出解决办法吗? @DineshBalasubramanian 这个特殊错误现在已经修复,但它是第 57 行供参考。 修正你的逻辑,你需要一个 currentColor 和 currentCrypto 的变量 【参考方案1】:

您收到错误[dart] 2 required argument(s) expected, but 0 found. 的原因是没有传递任何参数。您提供的另一个示例:_cryptoWidget(currency, color) - 您正在尝试传递一个与所需参数不匹配的列表。

如 cmets 中所述,您可以在这里为 List 定义索引以获取所需的值:_cryptoWidget(_currencies[index], _colors[index])

【讨论】:

感谢您的回复。尽管当我问这个问题时我的经验要少得多,但回想起来我真的看不出我是多么困惑。您的回答将在其他人的早期旅程中有所帮助。

以上是关于Flutter - 需要 2 个必需参数,但找到 0 个的主要内容,如果未能解决你的问题,请参考以下文章

位置参数太多:预期有 2 个,但找到了 3 个。尝试删除多余的参数 Flutter

预期 1 个位置参数,但找到 0 个 (Flutter)

错误:位置参数太多:允许 0 个,但找到 2 个

flutter --flow-control-collections 是必需的,但它们是吗?

Flutter:“位置参数太多:预期为 0,但找到了 1”

Flutter:位置参数过多:预期为0,但找到1。此处:AppBar(