[英]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.