Flutter GetX 导航:预期有 1 个位置参数,但找到了 0 个
我对 Dart 和一般编码都很陌生。我在 YouTube 上观看教程后编写了这段代码。在大多数情况下,我能够自己解决大部分问题,但我无法找出我最近的错误。错误消息针对“HomeScreen()”,我无法导航到另一个屏幕(TodoScreen())。它们与位置参数和未定义参数有关。任何帮助将不胜感激,我已经将我认为必要的代码留在了下面。谢谢。
我的主页
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getxtodoyapp/controllers/todoController.dart';
import 'package:getxtodoyapp/screens/todoScreen.dart';
class HomeScreen extends StatelessWidget {
// ignore: prefer_const_constructors_in_immutables
HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final TodoController todoController = Get.put(TodoController());
return Scaffold(
appBar: AppBar(
title: const Text("GetX Todo List"),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Get.to(TodoScreen());
},
child: Icon(Icons.add),
),
body: Container(
child: Obx(() => ListView.separated(
itemBuilder: (context, i) => ListTile(
title: Text(
"${todoController.todos[i].text}",
style: (todoController.todos[i].done)
? const TextStyle(
color: Colors.red,
decoration: TextDecoration.lineThrough)
: const TextStyle(color: Colors.black),
),
onTap: () {
Get.to(TodoScreen(i));
},
trailing: const Icon(Icons.chevron_right),
leading: Checkbox(
value: todoController.todos[i].done,
onChanged: (chandedVlaue) {
var changed = todoController.todos[i];
changed.done = chandedVlaue!;
todoController.todos[i] = changed;
},
),
),
separatorBuilder: (_, __) => Divider(),
itemCount: todoController.todos.length)),
),
);
}
}
我的待办事项屏幕
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getxtodoyapp/controllers/todoController.dart';
import 'package:getxtodoyapp/models/todo.dart';
class TodoScreen extends StatelessWidget {
final TodoController todoController = Get.find();
final int index;
TodoScreen(this.index, {Key? key}): super(key:key);
//TodoScreen({Key? key, this.index }) : super(key: key);
//TodoScreen(this.index);
@overrideWidget build(BuildContext context) {
String? text ='';
if(this.index != null){
text = todoController.todos[index].text;
}
TextEditingController textEditingController = TextEditingController();
return Scaffold(
body: Container(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
Expanded(
child: TextField(
controller: textEditingController,
autofocus: true,
decoration: const InputDecoration(
hintText: 'What do you want to accomplish',
border: InputBorder.none,
focusedBorder: InputBorder.none),
style: TextStyle(fontSize: 25.0),
keyboardType: TextInputType.multiline,
maxLines: 999,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {Get.back();},
child: const Text("Cancle"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
),
),
ElevatedButton(
onPressed: () {
todoController.todos.add(Todo(text: textEditingController.text), );
Get.back();
},
child: const Text("Add"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green),
),
)
],
),
],
),
),
);
}
}
时遇到的错误
error: 1 positional argument(s) expected, but 0 found. (not_enough_positional_arguments at [getxtodoyapp] lib\screens\homeScreen.dart:18)
info: Name source files using `lowercase_with_underscores`. (file_names at [getxtodoyapp] lib\controllers\todoController.dart:1)
info: Name source files using `lowercase_with_underscores`. (file_names at [getxtodoyapp] lib\screens\homeScreen.dart:1)
info: Prefer const with constant constructors. (prefer_const_constructors at [getxtodoyapp] lib\screens\homeScreen.dart:20)
info: Avoid unnecessary containers. (avoid_unnecessary_containers at [getxtodoyapp] lib\screens\homeScreen.dart:22)
info: Prefer const with constant constructors. (prefer_const_constructors at [getxtodoyapp] lib\screens\homeScreen.dart:46)
info: Name source files using `lowercase_with_underscores`. (file_names at [getxtodoyapp] lib\screens\todoScreen.dart:1)
info: The value of the local variable 'text' isn't used. (unused_local_variable at [getxtodoyapp] lib\screens\todoScreen.dart:18)
info: The operand can't be null, so the condition is always true. (unnecessary_null_comparison at [getxtodoyapp] lib\screens\todoScreen.dart:19)
info: Don't access members with `this` unless avoiding shadowing. (unnecessary_this at [getxtodoyapp] lib\screens\todoScreen.dart:19)
info: Prefer const with constant constructors. (prefer_const_constructors at [getxtodoyapp] lib\screens\todoScreen.dart:39)
info: Unused import: 'package:getxtodoyapp/screens/todoScreen.dart'. (unused_import at [getxtodoyapp] test\widget_test.dart:12)
错误我运行应用程序
Running Gradle task 'assembleDebug'...
lib/screens/homeScreen.dart:18:28: Error: Too few positional arguments: 1 required, 0 given.
Get.to(TodoScreen());
^
lib/screens/todoScreen.dart:10:1: Context: Found this candidate, but the arguments don't match.
TodoScreen(this.index, {Key? key}): super(key:key);
^^^^^^^^^^
FAILURE: Build failed with an exception.
* Where:
Script 'C:\src\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1070
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\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 13s
Exception: Gradle task assembleDebug failed with exit code 1
Android Doctor
C:\src\projects\getxtodoyapp>flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.8.1, on Microsoft Windows [Version 10.0.22000.493], locale en-IN)
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
X cmdline-tools component is missing
Run `path/to/sdkmanager --install "cmdline-tools;latest"`
See https://developer.android.com/studio/command-line for more details.
X Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[√] Chrome - develop for the web
[√] Android Studio (version 4.1)
[√] IntelliJ IDEA Community Edition (version 2020.3)
[√] VS Code (version 1.56.2)
[√] Connected device (3 available)
! Doctor found issues in 1 category.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先安装Android SDK命令行工具
然后运行命令 flutter doctor --android-licenses 并在需要时按 y
然后运行
flutter clean
命令获取包裹pubspec.yaml 文件。要解决此问题,请传递此行中的参数
first of all install Android SDK command-line tools
then run command flutter doctor --android-licenses and press y whenever it asks for
then run
flutter clean
command then get packages in pubspec.yaml file.to fix the issue pass the parameter in this line
你使用了不正确的方式。
更改为
已解决
you are using improper way.
Change To
Solved