Fiz o passo a passo do aula porem, não estou conseguindo fazer com que o contato seja salvo na lista, ja tentei solucionar mudando em 'contacts_list.dart' Stateless para Stateful e em floatingActionButton adicionar o setSate, porem nada deu certo.
import 'package:flutter/material.dart';
import 'package:projeto2/database/app_database.dart';
import 'package:projeto2/models/contact.dart';
import 'package:projeto2/srceens/contact_from.dart';
class ContactsList extends StatefulWidget {
const ContactsList({Key? key}) : super(key: key);
@override
State<ContactsList> createState() => _ContactsListState();
}
class _ContactsListState extends State<ContactsList> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Contacts'),
),
// inserido generics no FutureBuilder
body: FutureBuilder<List<Contact>>(
initialData: const [],
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: const [
CircularProgressIndicator(),
Text('Loading'),
],
),
);
case ConnectionState.active:
break;
case ConnectionState.done:
final List<Contact>? contacts = snapshot
.data; // usado null safe na lista pois é regra da nova versão do flutter
if (contacts != null) {
//feito if para validar nulo
return ListView.builder(
itemBuilder: (context, index) {
final Contact contact = contacts[index];
return _ContactItem(contact);
},
itemCount: contacts.length,
);
}
break;
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Text('Unknown error',
style: TextStyle(color: Colors.red, fontSize: 24.0)),
],
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context)
.push(
MaterialPageRoute(builder: (context) => const ContactForm()))
.then((value) {
setState(() {
widget.createState();
});
});
},
child: const Icon(
Icons.add,
),
),
);
}
}
class _ContactItem extends StatelessWidget {
final Contact contact;
const _ContactItem(this.contact);
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text(
contact.name,
style: const TextStyle(fontSize: 24.0),
),
subtitle: Text(
contact.accountNumber.toString(),
style: const TextStyle(fontSize: 16.0),
),
),
);
}
}