SILENMUS
SILENMUS

Reputation: 947

flutter, Firebase Firestore I can't add element in the list

i'm trying to add a userID in the _thumbnailList but it doesn't work. I have no idea

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']}');
      }
    });

...

I call the method like this.

// 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,
            );
          }

recommendUidList is List

after I print it, in console there is a just empty List like [ ]

you guys any idea about this bug ? any. clue?

Upvotes: 0

Views: 266

Answers (2)

SILENMUS
SILENMUS

Reputation: 947

I found the solution If someone has same problem , i hope it'll help

enter image description here

as you can see i used final that's why 😇 man... i wasted my 3hours

Upvotes: 0

Dan Gerchcovich
Dan Gerchcovich

Reputation: 525

Are you trying to upload a single document, directly to your firestore?

In that case use this source code here:

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
        });
  }

The text file that you are uploading to the firestore will contain the userID that you are trying to upload. In this case, the google firestore is acting as a remote blob storage container. It must contain a file, even it where using object storage, where the UserID was metadata to a file, you would still have to collect the entire file, then add the metadata, and upload it again as one whole object.

Upvotes: 1

Related Questions