简体   繁体   中英

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)

 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:

dependencies:
  cloud_firestore: ^0.13.5

Then execute the following:

From the terminal: Run flutter pub get.

OR

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.


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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM