Error: Too few positional arguments: 1 required, 0 given

Error: Too few positional arguments: 1 required, 0 given

:25:23: 错误:位置参数太少:需要 1 个,给定 0 个。 BottomTabBtn(imagePath: "assets/images/tab_home.png"), ^ lib/widgets/bottom_tabs.dart:37:3: 上下文:找到这个候选人,但参数不匹配。 BottomTabBtn(this.imagePath); ^^^^^^^^^^^^ lib/widgets/bottom_tabs.dart:26:23: 错误:位置参数太少:需要 1 个,给定 0 个。 BottomTabBtn(imagePath: "assets/images/tab_search.png"), ^ lib/widgets/bottom_tabs.dart:37:3: 上下文:找到这个候选人,但参数不匹配。 BottomTabBtn(this.imagePath); ^^^^^^^^^^^^ lib/widgets/bottom_tabs.dart:27:23: 错误:位置参数太少:需要 1 个,给定 0 个。 BottomTabBtn(imagePath: "assets/images/tab_saved.png"), ^ lib/widgets/bottom_tabs.dart:37:3: 上下文:找到这个候选人,但参数不匹配。 BottomTabBtn(this.imagePath); ^^^^^^^^^^^^ lib/widgets/bottom_tabs.dart:28:23: 错误:位置参数太少:需要 1 个,给定 0 个。 BottomTabBtn(imagePath: "assets/images/tab_logout.png"), ^ lib/widgets/bottom_tabs.dart:37:3: 上下文:找到这个候选人,但参数不匹配。 BottomTabBtn(this.imagePath); ^^^^^^^^^^^^^

失败:构建失败,出现异常。

Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

56 秒后构建失败 异常:Gradle 任务 assembleDebug 失败,退出代码为 1

这是我的代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class BottomTabs extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(12.0),
          topRight: Radius.circular(12.0)
        ),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            spreadRadius: 1.0,
            blurRadius: 30.0,
          )
        ]
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          BottomTabBtn(imagePath: "assets/images/tab_home.png"),
          BottomTabBtn(imagePath: "assets/images/tab_search.png"),
          BottomTabBtn(imagePath: "assets/images/tab_saved.png"),
          BottomTabBtn(imagePath: "assets/images/tab_logout.png"),
        ],
      ),
    );
  }
}

class BottomTabBtn extends StatelessWidget {
  final String? imagePath;
  BottomTabBtn(this.imagePath);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(
        vertical: 22.0,
        horizontal: 16.0
      ),
      child: Image(
        image: AssetImage(
          imagePath ?? "assets/images/tab_home.png"
        ),
        width: 22.0,
        height: 22.0,
      ),
    );
  }
}

您不需要为 BottomTabBtn 指定参数标签。而不是:

BottomTabBtn(imagePath: "assets/images/tab_home.png"),

使用:

BottomTabBtn("assets/images/tab_home.png"),

如果您真的想使用参数标签,请将您的构造函数声明更改为:

BottomTabBtn({this.imagePath});