Home > Mobile >  'Too many positional arguments: 0 expected, but 1 found.'
'Too many positional arguments: 0 expected, but 1 found.'

Time:06-30

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

class AuthForm extends StatefulWidget {
  const AuthForm({Key? key}) : super(key: key);

  @override
  State<AuthForm> createState() => _AuthFormState();
}

class _AuthFormState extends State<AuthForm> {
  //--------------------------------------------------
  final _formkey = GlobalKey<FormState>();
  var _email = '';
  var _password = '';
  var _username = '';
  bool isLoginPage = false;
  //--------------------------------------------------
  @override
  Widget build(BuildContext context) {
    return Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: ListView(
          children: [
            Container(
                padding: const EdgeInsets.only(left: 10, right: 10, top: 10),
                child: Form(
                  key: _formkey,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      if (!isLoginPage)
                        TextFormField(
                            keyboardType: TextInputType.emailAddress,
                            key: ValueKey('username'), validator: (value) {
                          if (value!.isEmpty) {
                            return 'Incorrect Username';
                          }
                          return null;
                        }, onSaved: (value) {
                          _username = value.toString();
                        },
                            decoration: InputDecoration(
                                border: OutlineInputBorder(
                                    borderRadius: BorderRadius.circular(8.0),
                                    borderSide:
                                        BorderSide()), // Outline InputBorder
                                labelText: "Enter Username",
                                labelStyle: GoogleFonts.roboto()),
                            TextFormField(
                              keyboardType: TextInputType.emailAddress,
                              key: ValueKey('email'),
                              validator: (value) {
                                if (value!.isEmpty || !value.contains('@')) {
                                  return 'Incorrect Email';
                                }
                                return null;
                              },
                              onSaved: (value) {
                                _email = value.toString();
                              },
                              decoration: InputDecoration(
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(8.0),
                                      borderSide: BorderSide()),
                                  labelText: "Enter Email",
                                  labelStyle: GoogleFonts.roboto()),
                              TextFormField(
                                  obscureText: true,
                                  keyboardType: TextInputType.emailAddress,
                                  key: ValueKey('password'),
                                  validator: (value) {
                                    if (value!.isEmpty) {
                                      return 'Incorrect Password';
                                    }
                                    return null;
                                  },
                                  onSaved: (value) {
                                    _password = value.toString();
                                  },
                                  decoration: InputDecoration(
                                      border: OutlineInputBorder(
                                          borderRadius:
                                              BorderRadius.circular(8.0),
                                          borderSide: BorderSide()),
                                      labelText: "Enter Password",
                                      labelStyle: GoogleFonts.roboto())),
                            ))
                    ],
                  ),
                ))
          ],
        ));
  }
}

May I know what must be changed in the above code to fix the error? I tried modifying some arguments but to no avail. Should I consider specifying the type of argument? It is showing the following output: Too many positional arguments: 0 expected, but 1 found. Try removing the extra positional arguments, or specifying the name for named arguments. Too many positional arguments: 0 expected, but 1 found. Try removing the extra positional arguments, or specifying the name for named arguments.

CodePudding user response:

You have added your second and third TextFormField() within the first one. I have modified your code to put it outside of the TextFormField(), before the Column() ends:

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

class AuthForm extends StatefulWidget {
  const AuthForm({Key? key}) : super(key: key);

  @override
  State<AuthForm> createState() => _AuthFormState();
}

class _AuthFormState extends State<AuthForm> {
  //--------------------------------------------------
  final _formkey = GlobalKey<FormState>();
  var _email = '';
  var _password = '';
  var _username = '';
  bool isLoginPage = false;
  //--------------------------------------------------
  @override
  Widget build(BuildContext context) {
    return Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: ListView(
          children: [
            Container(
                padding: const EdgeInsets.only(left: 10, right: 10, top: 10),
                child: Form(
                  key: _formkey,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      if (!isLoginPage)
                        TextFormField(
                            keyboardType: TextInputType.emailAddress,
                            key: ValueKey('username'), validator: (value) {
                          if (value!.isEmpty) {
                            return 'Incorrect Username';
                          }
                          return null;
                        }, onSaved: (value) {
                          _username = value.toString();
                        },
                            decoration: InputDecoration(
                                border: OutlineInputBorder(
                                    borderRadius: BorderRadius.circular(8.0),
                                    borderSide:
                                    BorderSide()), // Outline InputBorder
                                labelText: "Enter Username",
                                labelStyle: GoogleFonts.roboto()),
                            ),
                      TextFormField(
                        keyboardType: TextInputType.emailAddress,
                        key: ValueKey('email'),
                        validator: (value) {
                          if (value!.isEmpty || !value.contains('@')) {
                            return 'Incorrect Email';
                          }
                          return null;
                        },
                        onSaved: (value) {
                          _email = value.toString();
                        },
                        decoration: InputDecoration(
                            border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(8.0),
                                borderSide: BorderSide()),
                            labelText: "Enter Email",
                            labelStyle: GoogleFonts.roboto()),
                       
                      ), TextFormField(
                          obscureText: true,
                          keyboardType: TextInputType.emailAddress,
                          key: ValueKey('password'),
                          validator: (value) {
                            if (value!.isEmpty) {
                              return 'Incorrect Password';
                            }
                            return null;
                          },
                          onSaved: (value) {
                            _password = value.toString();
                          },
                          decoration: InputDecoration(
                              border: OutlineInputBorder(
                                  borderRadius:
                                  BorderRadius.circular(8.0),
                                  borderSide: BorderSide()),
                              labelText: "Enter Password",
                              labelStyle: GoogleFonts.roboto())),
                    ],
                  ),
                ))
          ],
        ));
  }
}
  • Related