Aparentemente, a função "findAll" está devolvendo nulo e não estou entendendo o motivo.
Código da lista de Contatos:
class ContactsList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Contatos'),
),
body: FutureBuilder<List<Contact>>(
initialData: List(),
future: Future.delayed(Duration(seconds: 0)).then((value) {
findAll();
}),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
break;
case ConnectionState.waiting:
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
Text('Carregando...')
],
),
);
break;
case ConnectionState.active:
break;
case ConnectionState.done:
if (snapshot.data != null) {
final List<Contact> contacts = snapshot.data;
ListView.builder(
itemBuilder: (context, index) {
final Contact contact = contacts[index];
return _ContactItem(contact);
},
itemCount: contacts.length,
);
} else {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('snapshot.data é nulo'),
],
),
);
}
break;
default:
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('Unknown error'),
],
),
);
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context)
.push(
MaterialPageRoute(
builder: (context) => ContactForm(),
),
)
.then(
(newContact) => debugPrint(newContact.toString()),
);
},
child: Icon(
Icons.add,
),
),
);
}
}
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,
),
),
),
);
}
}
Código da função findAll:
Future<List<Contact>> findAll() {
return createDatabase().then((db) {
return db.query('contacts').then((maps) {
final List<Contact> contacts = List();
for (Map<String, dynamic> map in maps) {
final Contact contact = Contact(
map['id'],
map['name'],
map['account_number'],
);
contacts.add(contact);
}
return contacts;
});
});
}
Link do Git: https://github.com/biiaFlor/bytebank_flutter/tree/feature/ManipulacaoContatos