Flutter GetX 导航:预期有 1 个位置参数,但未找到任何位置参数
2022-02-23
407
我对 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)),
),
);
}
}
我的 Todo 屏幕
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.
2个回答
您使用了不正确的方式。
final int index;
TodoScreen(this.index, {Key? key}): super(key:key);
更改为
final int? index;
TodoScreen( {this.index,Key? key}): super(key:key);
已解决
A. Sang
2022-02-26