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

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

我正在处理我的第一个 Flutter 项目,我正在构建一个登录页面,我创建了一个变量来存储 TextFormFieldController 但我收到了上述错误,因为我删除了构造函数。 当我 return 这个构造函数时,我无法声明一个全局变量来存储 TextFormFieldController。

这是我的代码:(登录页面):

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

这是 main.dart(我得到错误的地方):

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

您需要在 MaterialApp 之前删除 const :

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

如果您为 LoginScreen 小部件创建 const 构造函数,这将解决 MyApp 问题。但是下一期来自 var loginUsernameController = TextEditingController(); 而现在我们已经创建了 const LoginScreen({Key? key}) : super(key: key);

对于 const 构造函数 class,它需要 class 级别内的 final 个变量。

但是TextEditingController()本身就是一个non-const构造函数。

您还可以在 build 方法中初始化 loginUsernameController,当它是 StatelessWidget 并且对于 StatefulWidget 使用 initState.