flutterdartwidgetargumentsstate-management

How to resolve '1 positional argument(s) expected, but 0 found' in Flutter?


I'm working on a Flutter application and encountered an error with the TomatoIcon widget, which expects 1 positional argument. I'm not sure what argument I need to pass to it. Here's the context of how I'm using the TomatoIcon widget within a SingleChildScrollView.

body: SingleChildScrollView(
  scrollDirection: Axis.vertical,
  child: Column(
    children: [
      SizedBox(height: 40, child: HelloUser()),
      SizedBox(height: 68, child: ChipsBlog()),
      SizedBox(height: 80, child: TomatoIcon()), // Error occurs here
      SizedBox(height: 77, child: HomePageTimerUI()),
      SizedBox(height: 580, child: StartPomodoro()),
      SizedBox(height: 580, child: ToDoPage()),
      SizedBox(height: MediaQuery.of(context).size.height, child: FooterPomoworko()),
    ],
  ),
),

And here is the TomatoIcon class defined in tomato.dart:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../open_source/controllers/countdown_controller.dart';

class TomatoIcon extends StatefulWidget {
  final Rx<stateRound> state;
  const TomatoIcon(this.state, {Key? key}) : super(key: key);

  @override
  State<TomatoIcon> createState() => _TomatoIconState();
}

class _TomatoIconState extends State<TomatoIcon> {
  @override
  Widget build(BuildContext context) {
    return Obx(
      () => IconButton(
          onPressed: null,
          icon: widget.state.value == stateRound.done
              ? Image.asset('assets/icons/tomatoDone.png')
              : Image.asset('assets/icons/tomatoUndone.png')),
    );
  }
}

The error I'm facing is:

1 positional argument(s) expected, but 0 found. Try adding
 the missing arguments.

This error occurs when I try to instantiate TomatoIcon without any arguments, but based on the class definition, it expects a state parameter of type Rx<stateRound>.

I've tried looking for similar issues on Stack Overflow but couldn't find anything that addresses this specific problem. How should I properly instantiate the TomatoIcon widget? Any guidance or examples would be greatly appreciated.

Thank you for your help.


Solution

  • The TomatoIcon constructor reads

    final Rx<stateRound> state;
    const TomatoIcon(this.state, {Key? key}) : super(key: key);
    

    so you have to provide TomatoIcon with a Rx<stateRound>, like so:

    SizedBox(height: 80, child:  TomatoIcon(myRx)), // here you provide TomatoIcon with its positional argument
    

    More on this topic here:

    https://dart.dev/guides/language/language-tour#parameters