Olá,
Estou tendo este erro ao abrir a lista de contatos:
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was
FutureBuilder<List<Contact>>
package:bytebank02/screens/contacts_lista.dart:19
When the exception was thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 ContactsList.build.<anonymous closure>
package:bytebank02/screens/contacts_lista.dart:28
#2 _FutureBuilderState.build
package:flutter/…/widgets/async.dart:751
#3 StatefulElement.build
package:flutter/…/widgets/framework.dart:4744
#4 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4627
Código contacts_list:
class ContactsList extends StatelessWidget {
final List<Contact> contacts = List();
@override
Widget build(BuildContext context) {
// contacts.add(Contact(name: "Anderson", accountNumber: 12));
// contacts.add(Contact(name: "Duarte", accountNumber: 45));
// contacts.add(Contact(name: "Alves", accountNumber: 78));
return Scaffold(
appBar: AppBar(
title: Text("Contacts"),
),
body: FutureBuilder(
future: findAll(),
builder: (context, snapshot) {
final List<Contact> contacts = snapshot.data;
return ListView.builder(
itemBuilder: (context, index) {
final Contact contact = contacts[index];
return _ContactItem(contact: contact);
},
itemCount: contacts.length,
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context)
.push(
MaterialPageRoute(
builder: (context) => ContactForm(),
),
)
.then((newContact) {
debugPrint("Contato");
debugPrint(newContact.toString());
});
},
child: Icon(Icons.add),
),
);
}
}
class _ContactItem extends StatelessWidget {
final Contact contact;
const _ContactItem({
Key key,
this.contact,
}) : super(key: key);
@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 app_database:
Future<Database> createDatabase() {
return getDatabasesPath().then((dbPath) {
final String path = join(dbPath, 'bytebank02.db');
return openDatabase(path, onCreate: (db, version) {
db.execute('CREATE TABLE contacts('
'id INTEGER PRIMARY KEY, '
'name TEXT, '
'account_number INTEGER)');
}, version: 1);
});
}
Future<int> save(Contact contact) {
return createDatabase().then((db) {
final Map<String, dynamic> contactMap = Map();
contactMap['id'] = contact.id;
contactMap['name'] = contact.name;
contactMap['account_number'] = contact.accountNumber;
return db.insert('contacts', contactMap);
});
}
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(
id: map['id'],
name: map['name'],
accountNumber: map['account_number'],
);
contacts.add(contact);
}
return contacts;
});
});
}