Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter – "Invalid constant value" inside OnChange – Flutter

Im trying to do a very basic Log in app in Flutter. I’m trying to get the email and password from a TextField Widget with OnChange, but it gives me this error "Invalid constant value", inside of the function of OnChange. What can I do?. Here is the code:

import 'package:flutter/material.dart';

String email = '';
String password = '';

class Login extends StatelessWidget {
  const Login({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(20.0),
      padding: const EdgeInsets.all(10.0),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const <Widget>[
            Padding(
              padding: EdgeInsets.all(30.0),
              child: Text(
                'Welcome to the log in'
              ),
            ),
            Padding(
              padding: EdgeInsets.all(10.0),
              child: TextField(
                obscureText: true,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'email',
                ),
                onChanged: (text) {
                  email = text; //Error here
                },
              ),
            ),
            Padding(
              padding: EdgeInsets.all(10.0),
              child: TextField(
                obscureText: true,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'password',
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

The I tried to elimanate the const label from Column, but gives me the warning "Prefer const with constant constructors".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This const needs to be removed:

children: const <Widget>[

Then you can add const in other places. I’d guess that the IDE helps you out there. It could be used e.g. here padding: EdgeInsets.all(10.0), so the result will be:

padding: const EdgeInsets.all(10.0),

and so forth…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading