Firebase 未处理的异常:NoSuchMethodError:方法';addAdvogado';在null上调用了。颤振
我用这个错误研究了其他类似的问题,但我无法修复它。我不知道我做错了什么。 当我尝试将信息发送到表单时,我的应用程序未保存到数据库。 我应该怎么做才能纠正 我收到消息: 未处理的异常:NoSuchMethodError:方法“addAdvogado”无效 调用null。E/颤振(7418):接收器:零E/颤振(7418): 尝试调用:addAdvogado(“Advogado”的实例) 律师。dartFirebase 未处理的异常:NoSuchMethodError:方法';addAdvogado';在null上调用了。颤振,firebase,flutter,dart,null,nosuchmethoderror,Firebase,Flutter,Dart,Null,Nosuchmethoderror,我用这个错误研究了其他类似的问题,但我无法修复它。我不知道我做错了什么。 当我尝试将信息发送到表单时,我的应用程序未保存到数据库。 我应该怎么做才能纠正 我收到消息: 未处理的异常:NoSuchMethodError:方法“addAdvogado”无效 调用null。E/颤振(7418):接收器:零E/颤振(7418): 尝试调用:addAdvogado(“Advogado”的实例) 律师。dart class Advogado { final String id; final Stri
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,
};
}
}
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);
}
}
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;
}
}
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,
),
final _formAdvogado=GlobalKey();
最终地图_dadosForm={};
容器(
保证金:所有边缘套(10.0),
孩子:升起按钮(
onPressed:()异步{
if(_formAdvogado.currentState.validate()){
_formAdvogado.currentState.save();
等待advogadoProvider.addAdvogado(
阿沃加多(
诺姆:“诺姆”,
电子邮件:“电子邮件”,
telefone:“telefone”,
endereco:“endereco”,
numeroOAB:“numeroOAB”,
),
);
Navigator.pop(上下文);
}
},
子项:文本(“Enviar”),
颜色:Colors.cyan,
textColor:Colors.white,
),
api\u律师\u火基。飞镖
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,
};
}
}
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);
}
}
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;
}
}
类ApiFirebase{
//最终FirebaseFirestore _bd=FirebaseFirestore.instance;
最终未来_initialize=Firebase.initializeApp();
FirebaseFirestore _bd=FirebaseFirestore.instance;
最终字符串路径;
收集参考文献;
ApiFirebase(this.path){
ref=_bd.collection(路径);
}
未来的getColecaoDados(){
返回ref.get();
}
溪流ColeCaodados(){
返回参考快照();
}
未来的GetDocumentObjId(字符串id){
返回ref.doc(id.get();
}
未来删除文档O(字符串id){
返回ref.doc(id).delete();
}
未来添加文档(地图护墙板){
返回参考添加(护墙板);
}
未来atualizarDocumento(地图护墙板,字符串id){
返回参考文件(id).更新(dados);
}
}
积垢-数据库\u layer.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,
};
}
}
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);
}
}
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;
}
}
带有ChangeNotifier的DBAdvogado类{
APIFERBASE_APIFERBASE=定位器();
列出建议;
Future buscarAdvogados()异步{
var result=await_apiferbase.getColecaoDados();
advogados=
result.docs.map((doc)=>Advogado.fromMap(doc.data(),doc.id)).toList();
返回advogados;
}
Stream buscarAdvogadoAsStream(){
return _apiferbase.streamColecaoDados();
}
未来getAdvogadoById(字符串id)异步{
var doc=await\u apiferbase.getDocumentObjID(id);
返回Advogado.fromMap(doc.data(),doc.id);
}
Advogado(Advogado护墙板,字符串id)异步{
wait_apiFirebase.atualizarDocumento(dados.toJson(),id);
返回;
}
未来Advogado(Advogado护墙板)异步{
wait_apiferbase.addDocumento(dados.toJson());
返回;
}
}
一般来说,如果在null上调用“某物”时出现错误,则表示您正在调用“某物”的主题当前为null
你在一个地方只对一个变量使用了
addAdvogado
方法-在form\u layer.dart
上的advogadoProvider
。所以问题是:如何以及在哪里设置advogadoProvider
?是的,我是用葡萄牙语写的方法和变量的,因为我是巴西人。我希望我没有被搞糊涂读我的问题。