我正在开发 Flutter 应用程序,并遇到了
TomatoIcon
小部件的错误,该小部件需要 1 个位置参数。我不确定我需要传递什么参数。以下是我如何在 TomatoIcon
中使用 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()),
],
),
),
这是
TomatoIcon
中定义的 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')),
);
}
}
我面临的错误是:
1 positional argument(s) expected, but 0 found. Try adding
the missing arguments.
当我尝试在没有任何参数的情况下实例化
TomatoIcon
时,会发生此错误,但根据类定义,它需要一个 state
类型的 Rx<stateRound>
参数。
我尝试在 Stack Overflow 上寻找类似的问题,但找不到解决此特定问题的任何内容。我应该如何正确实例化
TomatoIcon
小部件?任何指导或示例将不胜感激。
谢谢您的帮助。
TomatoIcon
构造函数读取
final Rx<stateRound> state;
const TomatoIcon(this.state, {Key? key}) : super(key: key);
所以你必须为
TomatoIcon
提供 Rx<stateRound>
,如下所示:
SizedBox(height: 80, child: TomatoIcon(myRx)), // here you provide TomatoIcon with its positional argument
有关此主题的更多信息: