5

I'm working on my first Flutter project, I'm building a Login page, I created a variable to store a TextFormFieldController but I got the error above because I deleted the constructor. When I return this constructor I cant declare a global variable to store the TextFormFieldController.

this is my code : (the Login page) :

import 'package:flutter/material.dart';

class LoginScreen extends StatelessWidget {
  var loginUsernameController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Padding(
        padding: const Edge

Insets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              "Login",
              style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold),
            ),
            const SizedBox(
              height: 40,
            ),
            TextFormField(
              decoration: const InputDecoration(
                labelText: "Email Address",
                border: OutlineInputBorder(),
                prefixIcon: Icon(Icons.email),
              ),
              keyboardType: TextInputType.emailAddress,
            ),
            const SizedBox(
              height: 10,
            ),
    

    TextFormField(
              controller: TextEditingController(),
              obscureText: true,
              decoration: const InputDecoration(
                labelText: "Password",
                border: OutlineInputBorder(),
                prefixIcon: Icon(Icons.lock),
                suffixIcon: Icon(Icons.remove_red_eye),
              ),
              keyboardType: TextInputType.emailAddress,
            ),
            const SizedBox(
              height: 20,
            ),
            Container(
              width: double.infinity,
              child: MaterialButton(
                onPressed: () {},
                child: const Text(
                  "LOGIN",
                  style: TextStyle(color: Colors.white),
                ),
                color: Colors.blue,
              ),
            )
          ],
        ),
      ),
    );
  }
}

this is the main.dart (Where I got the error) :

import 'package:flutter/material.dart';

import 'login_screen.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: LoginScreen(),
    );
  }
}
Wahéb
  • 541
  • 1
  • 5
  • 15

2 Answers2

26

You need to remove const before MaterialApp :

return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: LoginScreen(),
    );
Diwyansh
  • 2,961
  • 1
  • 7
  • 11
  • 1
    It works thank you man! can you explain me what is the relationship between removing the const keyword and the error please ? – Wahéb Dec 30 '21 at 11:22
  • 3
    const keyword requires hard coded constant value but you are building dynamic widget which is not constant that's why it's throwing an error. Also please mark as answered if it worked. Thanks – Diwyansh Dec 30 '21 at 11:25
  • 1
    it's actualy works I haven't notice the const before Materials set me into problems – Faisal Mohammad Apr 26 '22 at 21:33
1

If you create const constructor for LoginScreen widget, that will resolve the MyApp issue. But the next issue comes from var loginUsernameController = TextEditingController(); while now we have created const LoginScreen({Key? key}) : super(key: key);

For const constructor class, it requires final variables inside class level.

But TextEditingController() itself is a non-const constructor.

You can also initialize loginUsernameController inside build method while it is StatelessWidget and for StatefulWidget use initState.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Thank you for your explanation! I have understand thank you a lot. – Wahéb Dec 30 '21 at 17:18
  • I have a question, I did what @Diwyansh said in the previous answer! is that the best practise ? (Removing the const keyword from the MaterialApp)... or the best is to create the variable inside the build method ? – Wahéb Dec 30 '21 at 17:19
  • 1
    It is recommended to use `const` when ever possible, it would be issue placing inside build if it was `statefullWidget`, but for cases like this, you like to pass through constructor – Md. Yeasin Sheikh Dec 30 '21 at 20:11