首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有火基连接的Crud应用程序(颤振)

具有火基连接的Crud应用程序(颤振)
EN

Stack Overflow用户
提问于 2022-11-25 03:59:54
回答 1查看 31关注 0票数 0

我学习颤振。我是初学者,目前开始学习CRUD与火柴。

我是从这位伟大的绅士守则中学习的

https://github.com/mohamedHassanKa/ProductAppCourse

这是我第一次连接火力基地。我是否成功地连接了火源?我犯了这些错误。谁能教我怎么改正密码吗?

CRUDEModel.dart

代码语言:javascript
复制
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 ;

  }

错误

代码语言:javascript
复制
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;
                        ^^^^^^^^^
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-25 04:22:00

既然您说您是新来的,那么您应该知道这个存储库是3年前创建的,发生了很多更新。

使用Firebase服务的新版本(确切地说是在Firestore中),在项目中获取它的实例:

代码语言:javascript
复制
Firestore.instance // old
FirebaseFirestore.instance // new

documents属性被docs替换,因此您需要使用以下内容:

代码语言:javascript
复制
 result.documents // old - not working
 result.docs // new - will return a List<DocumentSnapshot>

要获取特定文档的Map<String, dynamic>数据,需要:

代码语言:javascript
复制
 doc.data // old
 doc.data() as Map<String, dynamic> new

要获得文档的id,您应该使用:

代码语言:javascript
复制
doc.documentId // old
doc.id // new

因此,基本上,使用新迁移的代码应该类似于以下内容:

代码语言:javascript
复制
class CRUDModel extends ChangeNotifier {
  Api _api = locator<Api>();

  List<Product> products;

  Future<List<Product>> fetchProducts() async {
    QuerySnapshot result = await _api.getDataCollection();
    products = result.docs
        .map((doc) => Product.fromMap(doc.data() as Map<String, dynamic>, doc.id))
        .toList();
    return products;
  }

  Stream<QuerySnapshot> fetchProductsAsStream() {
    return _api.streamDataCollection();
  }

  Future<Product> getProductById(String id) async {
    DocumentSnapshot doc = await _api.getDocumentById(id);
    return  Product.fromMap(doc.data() as Map<String, dynamic>, doc.id) ;
  }


  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 ;

  }}

我假设这些更改是不够的,您需要使用Firebase的新术语/符号进行迁移。

我想说的是,您需要停止使用旧的存储库(无意冒犯它的所有者),寻找一个新的项目,或者只需先看一下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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74568204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档