【发布时间】:2020-10-02 08:38:54
【问题描述】:
我用这个错误研究了其他类似的问题,但我无法解决它。我不知道我做错了什么。 当我尝试将信息发送到表单时,我的应用程序没有保存到数据库中。 我该怎么做才能更正?
我收到消息:
未处理的异常:NoSuchMethodError:方法“addAdvogado”是 调用为空。 E/颤动(7418):接收器:空 E/颤动(7418): 尝试调用:addAdvogado(Instance of 'Advogado')
lawyer.dart
class Advogado {
final String id;
final String nome;
final String email;
final String telefone;
final String endereco;
final String numeroOAB;
const Advogado(
{this.id,
@required this.nome,
@required this.email,
@required this.telefone,
@required this.endereco,
@required this.numeroOAB});
Advogado.fromMap(Map snapshot, String id)
: id = snapshot['id'] ?? '',
nome = snapshot['nome'] ?? '',
email = snapshot['email'] ?? '',
telefone = snapshot['telefone'] ?? '',
endereco = snapshot['endereco'] ?? '',
numeroOAB = snapshot['numeroOAB'] ?? '';
toJson() {
return {
"id": id,
"nome": nome,
"email": email,
"telefone": telefone,
"endereco": endereco,
"numeroOAB": numeroOAB,
};
}
}
form_lawyer.dart - 示例代码
final _formAdvogado = GlobalKey<FormState>();
final Map<String, String> _dadosForm = {};
Container(
margin: EdgeInsets.all(10.0),
child: RaisedButton(
onPressed: () async {
if (_formAdvogado.currentState.validate()) {
_formAdvogado.currentState.save();
await advogadoProvider.addAdvogado(
Advogado(
nome: 'nome',
email: 'email',
telefone: 'telefone',
endereco: 'endereco',
numeroOAB: 'numeroOAB',
),
);
Navigator.pop(context);
}
},
child: Text("Enviar"),
color: Colors.cyan,
textColor: Colors.white,
),
api_lawyer_firebase.dart
class ApiFirebase {
// final FirebaseFirestore _bd = FirebaseFirestore.instance;
final Future<FirebaseApp> _initialize = Firebase.initializeApp();
FirebaseFirestore _bd = FirebaseFirestore.instance;
final String path;
CollectionReference ref;
ApiFirebase(this.path) {
ref = _bd.collection(path);
}
Future<QuerySnapshot> getColecaoDados() {
return ref.get();
}
Stream<QuerySnapshot> streamColecaoDados() {
return ref.snapshots();
}
Future<DocumentSnapshot> getDocumentoById(String id) {
return ref.doc(id).get();
}
Future<void> removerDocumento(String id) {
return ref.doc(id).delete();
}
Future<DocumentReference> addDocumento(Map dados) {
return ref.add(dados);
}
Future<void> atualizarDocumento(Map dados, String id) {
return ref.doc(id).update(dados);
}
}
CRUD - database_laywer.dart
class DBAdvogado with ChangeNotifier {
ApiFirebase _apiFirebase = locator<ApiFirebase>();
List<Advogado> advogados;
Future<List<Advogado>> buscarAdvogados() async {
var result = await _apiFirebase.getColecaoDados();
advogados =
result.docs.map((doc) => Advogado.fromMap(doc.data(), doc.id)).toList();
return advogados;
}
Stream<QuerySnapshot> buscarAdvogadoAsStream() {
return _apiFirebase.streamColecaoDados();
}
Future<Advogado> getAdvogadoById(String id) async {
var doc = await _apiFirebase.getDocumentoById(id);
return Advogado.fromMap(doc.data(), doc.id);
}
Future removerAdvogado(Advogado dados, String id) async {
await _apiFirebase.atualizarDocumento(dados.toJson(), id);
return;
}
Future addAdvogado(Advogado dados) async {
await _apiFirebase.addDocumento(dados.toJson());
return;
}
}
【问题讨论】:
标签: firebase flutter dart null nosuchmethoderror