Apenas converti para StatefulWidget e funcionou, não entendi como posso usar o setState().
class ContactList extends StatefulWidget {
@override
_ContactListState createState() => _ContactListState();
}
class _ContactListState extends State<ContactList> {
@override
Widget build(BuildContext context) {
//contacts.add(Contact(0, 'alex', 1000)); teste para aparecer na lista
return Scaffold(
appBar: AppBar(
title: Text('Contacts'),
),
body: FutureBuilder<List<Contact>>(
// não precisamos de initialData se tratarmos o erro de 'null'!
initialData: List(),
future: findAll(),
// tratando o null
//Future.delayed(Duration(seconds: 1)).then((value) => 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')
],
),
);
break;
case ConnectionState.active:
break;
case ConnectionState.done:
final List<Contact> contacts = snapshot.data;
return ListView.builder(
itemBuilder: (context, index) {
final Contact contact = contacts[index];
return _ContactItem(contact);
},
itemCount: contacts.length,
);
break;
}
return 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) {
// TODO: implement build
return Card(
child: ListTile(
title: Text(
contact.name,
style: TextStyle(
fontSize: 24.0,
),
),
subtitle: Text(
contact.accountNumber.toString(),
style: TextStyle(
fontSize: 16.0,
),
),
),
);
}
}