Flutter GetX 导航:预期有 1 个位置参数,但找到了 0 个

发布于 2025-01-09 08:33:50 字数 7488 浏览 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.

I am very new to Dart, and coding in general. I have produced this code after watching tutorials on YouTube. For the most part, I have been able to troubleshoot most of my problems on my own, yet I cannot figure out my most recent errors. The error messages are for 'HomeScreen()', I am unable to navigate to another screen (TodoScreen()). and they are related to positional arguments and undefined parameters. Any help would be greatly appreciated, I have left the code, which I believe is necessary, down below. Thanks.

My home Page

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

My Todo Screen

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 What I have

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)

Error When I run app

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉目亦如画i 2025-01-16 08:33:50

首先安装Android SDK命令行工具

install CDK command-line tools

然后运行命令 flutter doctor --android-licenses 并在需要时按 y

然后运行 ​​flutter clean 命令获取包裹pubspec.yaml 文件。

要解决此问题,请传递此行中的参数
修复问题

first of all install Android SDK command-line tools

install CDK 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
fix the issue

五里雾 2025-01-16 08:33:50

你使用了不正确的方式。

final int index;
TodoScreen(this.index, {Key? key}): super(key:key);

更改为

final int? index;
TodoScreen( {this.index,Key? key}): super(key:key);

已解决

you are using improper way.

final int index;
TodoScreen(this.index, {Key? key}): super(key:key);

Change To

final int? index;
TodoScreen( {this.index,Key? key}): super(key:key);

Solved

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文