我是初学者,现在开始学习使用火力基础的CRUD。
我从这位伟大的绅士的法典中学习
https://github.com/mohamedHassanKa/ProductAppCourse
这是我第一次连接firebase。我连接firebase成功了吗?我得到了这些错误。谁能教我如何更正代码吗?
CRUDEModel.dart
import 'dart:async';
import 'package:flutter/material.dart';
import '../../locator.dart';
import '../services/api.dart';
import '../models/productModel.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class CRUDModel extends ChangeNotifier {
Api _api = locator<Api>();
List<Product> products;
Future<List<Product>> fetchProducts() async {
var result = await _api.getDataCollection();
products = result.documents
.map((doc) => Product.fromMap(doc.data, doc.documentID))
.toList();
return products;
}
Stream<QuerySnapshot> fetchProductsAsStream() {
return _api.streamDataCollection();
}
Future<Product> getProductById(String id) async {
var doc = await _api.getDocumentById(id);
return Product.fromMap(doc.data, doc.documentID) ;
}
Future removeProduct(String id) async{
await _api.removeDocument(id) ;
return ;
}
Future updateProduct(Product data,String id) async{
await _api.updateDocument(data.toJson(), id) ;
return ;
}
Future addProduct(Product data) async{
var result = await _api.addDocument(data.toJson()) ;
return ;
}
错误
lib/core/viewmodels/CRUDModel.dart:16:23: Error: The getter 'documents' isn't defined for the class 'QuerySnapshot<Object>'.
- 'QuerySnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('../../Documents/flutter_windows_3.3.4-stable/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-4.1.0/lib/cloud_firestore.dart').
- 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing getter, or defining a getter or field named 'documents'.
products = result.documents
^^^^^^^^^
lib/core/viewmodels/CRUDModel.dart:28:43: Error: The getter 'documentID' isn't defined for the class 'DocumentSnapshot<Object>'.
- 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('../../Documents/flutter_windows_3.3.4-stable/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-4.1.0/lib/cloud_firestore.dart').
- 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing getter, or defining a getter or field named 'documentID'.
return Product.fromMap(doc.data, doc.documentID) ;
^^^^^^^^^^
lib/core/viewmodels/CRUDModel.dart:28:33: Error: The argument type 'Object Function()' can't be assigned to the parameter type 'Map<dynamic, dynamic>'.
- 'Object' is from 'dart:core'.
- 'Map' is from 'dart:core'.
return Product.fromMap(doc.data, doc.documentID) ;
^
lib/core/services/api.dart:5:9: Error: Type 'Firestore' not found.
final Firestore _db = Firestore.instance;
^^^^^^^^^
lib/core/services/api.dart:5:9: Error: 'Firestore' isn't a type.
final Firestore _db = Firestore.instance;
^^^^^^^^^
lib/core/services/api.dart:5:25: Error: Undefined name 'Firestore'.
final Firestore _db = Firestore.instance;
^^^^^^^^^
1条答案
按热度按时间8hhllhi21#
既然你说你是新手,那么你应该知道这个库是3年前建立的,并且Flutter的Firebase SDK发生了很多更新。
使用新版本的Firebase服务,在Firestore中,将其示例放入项目中:
documents
属性将替换为docs
,因此您需要使用以下代码:要获取特定文档的
Map<String, dynamic>
数据,您需要:要获取文档的
id
,应使用:因此基本上,包含新迁移的代码应该类似于:
我认为这些更改还不够,您需要使用Firebase SDK的新术语/符号进行迁移。
我想说的是,您需要停止使用旧的存储库(无意冒犯其所有者),并寻找一个新的项目,或者只是先遵循并阅读Firebase的官方文档。
请检查以下内容:
https://firebase.flutter.dev/docs/firestore/overview
https://firebase.flutter.dev/
https://firebase.google.com/docs/flutter/setup
https://firebase.flutter.dev/docs/firestore/2.0.0_migration