未处理的异常:NoSuchMethodError:在null上调用了方法'addAdvogado'在Flutter

问题描述

我针对此错误研究了其他类似的问题,但无法解决。我不知道我在做什么错。 当我尝试将信息发送到表单时,我的应用程序没有保存到数据库。 我该怎么做才能纠正?

我收到消息:

未处理的异常:NoSuchMethodError:方法“ addAdvogado”为 调用为null。 E / flutter(7418):接收方:空E / flutter(7418): 尝试调用:addAdvogado(“ 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;
  }
}

解决方法

通常,当“某事”在null上出现错误时,这意味着您正在呼叫“ something”的主题目前为空。

您只能在一个变量的一个位置上使用addAdvogado方法-在form_lawyer.dart的{​​{1}}中。因此,问题是:如何以及在哪里设置advogadoProvider

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...