Flutter error : The constructor being called isn't a const constructor
Asked Answered
P

2

10

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(),
    );
  }
}
Principal answered 30/12, 2021 at 11:17 Comment(0)
N
39

You need to remove const before MaterialApp :

return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: LoginScreen(),
    );
Nihil answered 30/12, 2021 at 11:20 Comment(3)
It works thank you man! can you explain me what is the relationship between removing the const keyword and the error please ?Yarmouth
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. ThanksNihil
it's actualy works I haven't notice the const before Materials set me into problemsGlobeflower
D
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.

Discus answered 30/12, 2021 at 11:34 Comment(3)
Thank you for your explanation! I have understand thank you a lot.Yarmouth
I have a question, I did what @Nihil 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 ?Yarmouth
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 constructorDiscus

© 2022 - 2025 — McMap. All rights reserved.