Home > Net >  Flutter error : The constructor being called isn't a const constructor
Flutter error : The constructor being called isn't a const constructor

Time:12-30

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

CodePudding user response:

You need to remove const before MaterialApp :

return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: LoginScreen(),
    );

CodePudding user response:

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.

  • Related