0
votes

[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';
}
1
can you add code where you are working with firebase. if you added package while your app was running then try flutter clean and then again run flutter run.Viren V Varasadiya
nothing happened on running comands:- flutter clean & flutter runVairochan Sah
can you add code?Viren V Varasadiya
added the code ...Vairochan Sah

1 Answers

0
votes

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.