简体   繁体   English

连接到 Firestore 时出错:flutter,MissingPluginException

[英]Error Connecting to Firestore: flutter, MissingPluginException

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method DocumentReference#setData on channel plugins.flutter.io/cloud_firestore) [错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:MissingPluginException(在通道插件上找不到方法 DocumentReference#setData 的实现。flutter.io/cloud_firestore)

 Future<void> _createJob(BuildContext context) async{
    final database = Provider.of<Database>(context);
    await database.createJob(Job(name: 'Bloging', ratePerHour: 10));

  }
import 'package:flutter/foundation.dart';

class Job{
  Job({@required this.name, @required this.ratePerHour});
  final String name;
  final int ratePerHour;
  Map<String, dynamic> toMap() {
    return{
      'name': name,
      'ratePerHour': ratePerHour,
    };

  }

}
               create: (_) => FirestoreDatabase(uid: user.uid),
               child: JobsPage());
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:time_tracker/app/models/jobs.dart';
import 'dart:async';

import 'package:time_tracker/servises/api_path.dart';

abstract class Database {
  Future<void> createJob (Job job);
}
class FirestoreDatabase implements Database {
  FirestoreDatabase({@required this.uid}) : assert(uid != null);
  final String uid;

  Future<void> createJob (Job job) async {
    final path = APIPAth.job(uid, 'job_abc');
    final documentReference = Firestore.instance.document(path);
    await documentReference.setData(job.toMap());
  }
}
  static String job (String uid, String jobId) => '/users/$uid/jobs/$jobId';
}

Add the firestore plugin to pubspec.yaml file:将 firestore 插件添加到pubspec.yaml文件:

dependencies:
  cloud_firestore: ^0.13.5

Then execute the following:然后执行以下命令:

From the terminal: Run flutter pub get.从终端:运行 flutter pub get。

OR或者

From Android Studio/IntelliJ: Click Packages get in the action ribbon at the top of pubspec.yaml.从 Android Studio/IntelliJ:单击 Packages 进入 pubspec.yaml 顶部的操作功能区。

From VS Code: Click Get Packages located in right side of the action ribbon at the top of pubspec.yaml.从 VS Code:单击位于 pubspec.yaml 顶部操作功能区右侧的 Get Packages。


In your code change:在您的代码更改中:

 final documentReference = Firestore.instance.document(path);
    await documentReference.setData(job.toMap());

into this:进入这个:

 final documentReference = Firestore.instance.collection(path);
    await documentReference.add(job.toMap());

In firestore you have:在 Firestore 中,您有:

collection->document->collection->document集合->文档->集合->文档

If you want to add data to a collection , then you need to use the add() method which will create a random document id for and add the data.如果你想将数据添加到collection中,那么你需要使用add()方法,该方法将创建一个随机文档 ID 并添加数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter Firestore 错误:MissingPluginException(找不到方法 DocumentReference#updateData 的实现 - Flutter Firestore Error: MissingPluginException(No implementation found for method DocumentReference#updateData flutter cloud-firestore MissingPluginException 异常 - flutter cloud-firestore MissingPluginException Exception flutter MissingPluginException 上的 Firebase 初始化错误 - Firebase initialization error on flutter MissingPluginException Firebase 模拟器 Firestore 未连接到 flutter - Firebase Emulator Firestore not connecting to flutter MissingPluginException(未找到方法 DocumentReference#get on channel plugins.flutter.io/firebase_firestore 的实现) - MissingPluginException(No implementation found for method DocumentReference#get on channel plugins.flutter.io/firebase_firestore) 将数据从 flutter 隔离发送到 Firestore 上的 MissingPluginException(找不到方法 DocumentReference 的实现) - MissingPluginException(No implementation found for method DocumentReference) on Send data to firestore from flutter isolate 将 Firestore 模拟器连接到 flutter_test - Connecting Firestore emulator to flutter_test I/flutter (22027): MissingPluginException(在通道 plugins.flutter.io/cloud_firestore 上找不到方法 DocumentReference#setData 的实现) - I/flutter (22027): MissingPluginException(No implementation found for method DocumentReference#setData on channel plugins.flutter.io/cloud_firestore) Flutter Firebase 实时数据库 MissingPluginException - Flutter Firebase Realtime Database MissingPluginException 无法在 Flutter 中集成 Firebase - MissingPluginException - Cannot integrate Firebase in Flutter - MissingPluginException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM