Flutter GetX Navigation: 1 positional argument(s) expected, but 0 found

Viewed 231

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.
2 Answers

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

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

Related