连接到 Firestore 时出错:flutter,MissingPluginException

Error Connecting to Firestore: flutter, MissingPluginException

[错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:MissingPluginException(未在通道 plugins.flutter.io/cloud_firestore 上找到方法 DocumentReference#setData 的实现)

 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';
}

将 firestore 插件添加到 pubspec.yaml 文件:

dependencies:
  cloud_firestore: ^0.13.5

然后执行以下:

From the terminal: Run flutter pub get.

From Android Studio/IntelliJ: Click Packages get in the action ribbon at the top of pubspec.yaml.

From VS Code: Click Get Packages located in right side of the action ribbon at the top of pubspec.yaml.


在您的代码更改中:

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

进入这个:

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

在 firestore 中你有:

collection->document->collection->document

如果您想将数据添加到 collection,则需要使用 add() 方法,该方法会创建一个随机文档 ID 并添加数据。