1
resposta

Controller está sendo acusado como valor constante inválido.

Erro nos controller.

// ignore_for_file: prefer_const_constructors
import 'package:bytebankoffi/models/contact.dart';
import 'package:flutter/material.dart';

class ContactForm extends StatefulWidget {
  const ContactForm({Key? key}) : super(key: key);

  @override
  _ContactFormState createState() => _ContactFormState();
}

class _ContactFormState extends State<ContactForm> {
final TextEditingController _nameController = TextEditingController();
final TextEditingController _accountNumberController =
      TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Novo Contato'),
        backgroundColor: Colors.green[900],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            const TextField(
              controller: _nameController,
              decoration: InputDecoration(
                labelText: 'Fullname',
              ),
              style: TextStyle(
                fontSize: 24.0,
              ),
              keyboardType: TextInputType.number,
            ),
            Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: const TextField(
                controller: _accountNumberController,
                decoration: InputDecoration(
                  labelText: 'Account Number',
                ),
                style: TextStyle(
                  fontSize: 24.0,
                ),
                keyboardType: TextInputType.number,
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 16.0),
              child: SizedBox(
                width: double.maxFinite,
                child: ElevatedButton(
                  child: Text('Create'),
                  onPressed: () {
                    final String name = _nameController.text;
                    final int? accountNumber = int.tryParse(_accountNumberController.text);
                    final Contact newContact = Contact(0, name, accountNumber);
                    Navigator.pop(context, newContact);
                  },
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
![](![Erro acusado pelo editor.](https://cdn1.gnarususercontent.com.br/1/405760/79575eac-00f4-48ce-a0b5-c4f329656783.png)  )
1 resposta

Este erro ocorre porque você esta declarando o TextField como constante, mas o controller que esta sendo usado no TextField não é uma constante.

O código final ficaria assim

TextField(
    controller: _nameController,
    decoration: const InputDecoration(
        labelText: 'Fullname',
    ),
    style: const TextStyle(
        fontSize: 24.0,
    ),
    keyboardType: TextInputType.number,
),