Home > front end >  Flutter - "Invalid constant value" inside OnChange - Flutter
Flutter - "Invalid constant value" inside OnChange - Flutter

Time:11-21

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".

CodePudding user response:

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...

  • Related