首页 > 解决方案 > 颤动,Firebase Firestore 我无法在列表中添加元素

问题描述

我正在尝试在 _thumbnailList 中添加用户 ID,但它不起作用。我不知道

List<DocumentSnapshot> _thumbnailList;
List<DocumentSnapshot> get thumbnailList => _thumbnailList;

...

  void addRecommendListLocalAndServerInSearchProduct(
      {@required String productId,
      @required String loginUserUid,
      @required List<Product> productLIst}) async {

    List<dynamic> recommendList = [];
    String uploaderId;

    _thumbnailList.forEach((element) {
      if (element.data['productId'] == productId) {
        print(
            'thumbnail recommendUidList is  ${element.data['recommendUidList']}');
        ✅ element.data['recommendUidList'].add('dasda'); //for testing 
        print('︎thumbnail after added ${element.data['recommendUidList']}');
      }
    });

...

我这样称呼方法。

// user already recommend this
          if (_isRecommend) {
            setState(() => _isRecommend = false);
            productsProvider.removeRecommendListLocalAndServerInSearchProduct(
              productId: _productId,
              productLIst: _loadedProducts,
              loginUserUid: _loginUserId,
            );
          }

// user haven't recommend this 
          else {
            setState(() => _isRecommend = true);
            productsProvider.addRecommendListLocalAndServerInSearchProduct(
              productId: _productId,
              loginUserUid: _loginUserId,
              productLIst: _loadedProducts,
            );
          }

推荐UidList 是列表

在我打印它之后,在控制台中有一个像 [ ] 这样的空列表

你们对这个错误有任何想法吗?任何。线索?

标签: firebasefluttergoogle-cloud-firestore

解决方案


您是否尝试将单个文档直接上传到您的 Firestore?

在这种情况下,请在此处使用此源代码:

import 'dart:io';
import 'dart:typed_data';

import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
import 'package:path/path.dart';

typedef OnCompleteFirebaseUpload = Function(String cloudPath);

void uploadDocument(String filePath, OnCompleteFirebaseUpload complete,
      Function onError) async {
    firebase_storage.Reference firebaseDocument =
        firebase_storage.FirebaseStorage.instance.ref(
            'sample_directory/${basenameWithoutExtension(filePath)}_${DateTime.now().toIso8601String()}.txt');
    firebaseDocument
        .putFile(File(filePath))
        .whenComplete(
            () async => complete(await firebaseDocument.getDownloadURL()))
        .catchError((e){
          //Manage your error here
        });
  }

您上传到 Firestore 的文本文件将包含您尝试上传的用户 ID。在这种情况下,google firestore 充当远程 blob 存储容器。它必须包含一个文件,即使它使用对象存储,其中 UserID 是文件的元数据,您仍然必须收集整个文件,然后添加元数据,然后将其作为一个完整对象再次上传。


推荐阅读