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

Issue

:25:23: Error: Too few positional arguments: 1 required, 0 given.
BottomTabBtn(imagePath: "assets/images/tab_home.png"),
^
lib/widgets/bottom_tabs.dart:37:3: Context: Found this candidate, but the arguments don’t match.
BottomTabBtn(this.imagePath);
^^^^^^^^^^^^
lib/widgets/bottom_tabs.dart:26:23: Error: Too few positional arguments: 1 required, 0 given.
BottomTabBtn(imagePath: "assets/images/tab_search.png"),
^
lib/widgets/bottom_tabs.dart:37:3: Context: Found this candidate, but the arguments don’t match.
BottomTabBtn(this.imagePath);
^^^^^^^^^^^^
lib/widgets/bottom_tabs.dart:27:23: Error: Too few positional arguments: 1 required, 0 given.
BottomTabBtn(imagePath: "assets/images/tab_saved.png"),
^
lib/widgets/bottom_tabs.dart:37:3: Context: Found this candidate, but the arguments don’t match.
BottomTabBtn(this.imagePath);
^^^^^^^^^^^^
lib/widgets/bottom_tabs.dart:28:23: Error: Too few positional arguments: 1 required, 0 given.
BottomTabBtn(imagePath: "assets/images/tab_logout.png"),
^
lib/widgets/bottom_tabs.dart:37:3: Context: Found this candidate, but the arguments don’t match.
BottomTabBtn(this.imagePath);
^^^^^^^^^^^^

FAILURE: Build failed with an exception.

  • Where:
    Script ‘C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle’ line: 1035

  • What went wrong:
    Execution failed for task ‘:app:compileFlutterBuildDebug’.

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

  • Try:
    Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output. Run with –scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 56s
Exception: Gradle task assembleDebug failed with exit code 1

Here is my code:

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,
      ),
    );
  }
}

Solution

You don’t need to specify the parameter label for BottomTabBtn. Instead of:

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

Use:

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

If you really want to use parameter label, then change your constructor declaration to:

BottomTabBtn({this.imagePath});

Answered By – Midhun MP

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *