1
resposta

The method 'setState' isn't defined for the type 'ContactsList'.

Meu aplicativo está com um problema que quando eu escrevo o nome da pessoa e o numero da conta e clico em CREATE ao inves dele criar esse novo contato na parte de formulário ele volta pra tela principal do aplicativo, alguém pode me ajudar com isso?

estou com esse erro no meu contact_list.dart: "The method 'setState' isn't defined for the type 'ContactsList'. Try correcting the name to the name of an existing method, or defining a method named 'setState'.dart"

MEU CÓDIGO:

import 'package:flutter/material.dart';
import 'contact_form.dart';
import 'package:bytebank/models/contact.dart';
import 'package:bytebank/database/app_database.dart';

class ContactsList extends StatelessWidget {
  final List contacts = List.empty(growable: true);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:
          AppBar(title: Text('Contacts'), backgroundColor: Colors.green[900]),
      body: FutureBuilder<List<Contact>>(
        initialData: List.empty(growable: true),
        future: findAll(),
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              break;
            case ConnectionState.waiting:
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    CircularProgressIndicator(),
                    Text('Loading')
                  ],
                ),
              );
            case ConnectionState.active:
              break;
            case ConnectionState.done:
              final List<Contact> contacts =
                  snapshot.data is Object ? snapshot.data as List<Contact> : [];
              return ListView.builder(
                itemBuilder: (context, index) {
                  final Contact contact = contacts[index];
                  return _ContactItem(contact);
                },
                itemCount: contacts.length,
              );
          }
          return Text('Unknown error');
        },
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Theme.of(context).primaryColor,
        child: const Icon(
          Icons.add,
        ),
        onPressed: () {
          Navigator.of(context)
              .push(
                MaterialPageRoute(
                  builder: (context) => ContactForm(),
                ),
              )
              .then(
                (value) => setState(() {}),
              );
        },
      ),
    );
  }


}

class _ContactItem extends StatelessWidget {
  final Contact contact;
  _ContactItem(this.contact);

  @override
  Widget build(BuildContext context) {
    return Card(
      child: ListTile(
        title: Text(
          contact.name,
          style: TextStyle(fontSize: 24.0),
        ),
        subtitle: Text(
          contact.accountNumber.toString(),
          style: TextStyle(fontSize: 16.0),
        ),
      ),
    );
  }
}
1 resposta

Olá Gabriel, alguns fatores podem levar a isso. Vou colocar o código do elevatedButton aqui:

child: ElevatedButton(
                      onPressed: () {
                        final String name = _nameController.text;
                        final int? accountNumber =
                            int.tryParse(_accountController.text);
                        final Contact newContact =
                            Contact(0, name, accountNumber!);
                        _dao.save(newContact).then((id) => Navigator.pop(context));
                      },
                      child: Text('Create'))),

O elevatedButton deveria estar dentro da class ContactForm que herda de um StatefulWidget. Ao fazr o pop, ele retira a tela contact_form e retorna para contacts_list. Esepro que ajude! :)