[英]Unhandled Exception: NoSuchMethodError: The method 'addAdvogado' was called on null. On Flutter
I researched other similar questions with this error but I couldn't fix it.我用这个错误研究了其他类似的问题,但我无法修复它。 I don't know what I'm doing wrong.
我不知道我做错了什么。 My application is not saving to the database when I try to send the information to the form.
当我尝试将信息发送到表单时,我的应用程序没有保存到数据库中。 What should I do to correct?
我该怎么做才能纠正?
I receive the message:我收到消息:
Unhandled Exception: NoSuchMethodError: The method 'addAdvogado' was called on null.
未处理的异常:NoSuchMethodError:在 null 上调用了方法“addAdvogado”。 E/flutter ( 7418): Receiver: null E/flutter ( 7418): Tried calling: addAdvogado(Instance of 'Advogado')
E/flutter (7418): Receiver: null E/flutter (7418): 尝试调用: addAdvogado(Instance of 'Advogado')
lawyer.dart律师.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 - Sample code 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 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 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;
}
}
In general, when there is an error with "something" was called on null it means that the subject on which you are calling "something" is currently null.通常,当在 null上调用“something”出现错误时,这意味着您调用“something”的主题当前为 null。
You use addAdvogado
method in one place on only one variable - in form_lawyer.dart
on advogadoProvider
.您只在一个变量的一个地方使用
addAdvogado
方法 - 在form_lawyer.dart
上的advogadoProvider
。 So the question is: how and where do you set advogadoProvider
?所以问题是:您如何以及在哪里设置
advogadoProvider
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.