user19402977
user19402977

Reputation: 25

The method 'doc' isn't defined for the type 'CollectionReference'

I am getting a few error in my project. Can you help me?

The argument type 'StreamTransformer<dynamic, dynamic>' can't be assigned to the parameter type 'StreamTransformer<QuerySnapshot*, List>'.

The method 'doc' isn't defined for the type 'CollectionReference'. Try correcting the name to the name of an existing method, or defining a method named 'doc'.

The getter 'fromJson' isn't defined for the type 'Buy'. Try importing the library that defines 'fromJson', correcting the name to the name of an existing getter, or defining a getter or field named 'fromJson'.

The above error appears in the code below (1)

(1). firebase_api.dart

import 'package:flutter/material.dart';
import 'package:recipe_collector/ToDo/widget/Utils.dart';
import '../../Data/Model/BuyingListStore.dart';

class FirebaseApi {
  static Future<String> createBuy(Buy buy) async {
    final docBuy = Firestore.instance.collection('cart').doc();
                                                         ^^^
    buy.cID = docBuy.cID;
    await docBuy.set(buy.toJson());

    return docBuy.cID;
  }

  static Stream<List<Buy>> readBuyingList() => Firestore.instance
      .collection('todo')
      .orderBy(BuyingField.createdTime, descending: true)
      .snapshots()
      .transform(Utils.transformer(Buy.fromJson));
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  static Future updateBuyingList(Buy buy) async {
    final docTodo = Firestore.instance.collection('cart').doc(buy.cID);
                                                          ^^^

    await docTodo.update(buy.toJson());
  }

  static Future deletedBuyingList(Buy buy) async {
    final docTodo = Firestore.instance.collection('cart').doc(buy.cID);
                                                          ^^^
    await docTodo.delete();
  }
}

The getter 'docs' isn't defined for the type 'QuerySnapshot'. Try importing the library that defines 'docs', correcting the name to the name of an existing getter, or defining a getter or field named 'docs'.

The above error appears in the code below (2)

(2). Utils.dart

import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Utils {
  static void showSnackBar(BuildContext context, String text) =>
      ScaffoldMessenger.of(context)
        ..removeCurrentSnackBar()
        ..showSnackBar(SnackBar(content: Text(text)));

  static DateTime toDateTime(Timestamp value) {
    if (value == null) {
      return null!;
    }
    return value.toDate();
  }

  static dynamic fromDateTimeToJson(DateTime date) {
    if (date == null) return null;

    return date.toUtc();
  }

  static StreamTransformer transformer<T>(
          T Function(Map<String, dynamic> json) fromJson) =>
      StreamTransformer<QuerySnapshot, List<T>>.fromHandlers(
        handleData: (QuerySnapshot data, EventSink<List<T>> sink) {
          final snaps = data.docs.map((doc) => doc.data()).toList();
                             ^^^^
          final objects = snaps.map((json) => fromJson(json)).toList();
          sink.add(objects);
        },
      );
}

Upvotes: 0

Views: 54

Answers (0)

Related Questions