Ao clicar no botão para criar o novo contato, ocorre o seguinte erro:
The method 'then' was called on null.
Receiver: null
Tried calling: then<bool>(Closure: (int) => bool)
Não executa o pop do navigator, voltando para lista o contato foi salvo
código do botão
RaisedButton(
child: Text('Create'),
onPressed: () {
final String name = _nameController.text;
final int accountNumber =
int.tryParse(_accountNumberController.text);
final Contact newContact = Contact(0, name, accountNumber);
save(newContact).then((id) => Navigator.pop(context));
},
),
Código do save
Future<int> save(Contact contact) {
createDatabase().then((db) {
final Map<String, dynamic> contactMap = Map();
contactMap['name'] = contact.name;
contactMap['account_number'] = contact.accountNumber;
return db.insert('contacts', contactMap);
});
}
Código completo do form
import 'package:flutter/material.dart';
import 'package:gringotts/database/app_database.dart';
import 'package:gringotts/models/contact.dart';
class ContactForm extends StatefulWidget {
@override
_ContactFormState createState() => _ContactFormState();
}
class _ContactFormState extends State<ContactForm> {
final TextEditingController _nameController = TextEditingController();
final TextEditingController _accountNumberController =
TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('New Contact'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: 'Full Name',
),
style: TextStyle(
fontSize: 24.0,
)),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: TextField(
controller: _accountNumberController,
decoration: InputDecoration(
labelText: 'Account number',
),
style: TextStyle(
fontSize: 24.0,
),
keyboardType: TextInputType.number,
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: SizedBox(
width: double.maxFinite,
child: RaisedButton(
child: Text('Create'),
onPressed: () {
final String name = _nameController.text;
final int accountNumber =
int.tryParse(_accountNumberController.text);
final Contact newContact = Contact(0, name, accountNumber);
save(newContact).then<bool>((id) => Navigator.pop(context));
},
),
),
),
],
),
),
);
}
}