我是新手,请多多包涵,我正尝试在卡片上显示用户创建的信息。我将该信息存储在Firestore中,我想检索它。
我的firebase云存储的组织方式如下: posts / currentUserId / usersPost / INFO
但是我不知道如何在与云交互时在currentUsersId中增加价值。所以说文档旁边是currentUserId,但是我找不到如何增加价值。
import 'package:flutter/material.dart';
import 'package:expandable_card/expandable_card.dart';
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'post_details.dart';
class FeedScreen extends StatefulWidget {
static final String id = 'feed_screen';
final String userId;
FeedScreen({this.userId});
@override
_FeedScreenState createState() => _FeedScreenState();
}
class _FeedScreenState extends State<FeedScreen> {
StreamSubscription<QuerySnapshot> subscription;
List<DocumentSnapshot> snapshot;
CollectionReference collectionReference =
Firestore.instance.collection('posts').document(**CurrentUserId**).collection('usersPosts');
passData(DocumentSnapshot snap) {
Navigator.of(context).push(new MaterialPageRoute(
builder: (context) => PostDetails(
snapshot: snap,
)));
}
@override
void initState() {
super.initState();
subscription = collectionReference.snapshots().listen((datasnapshot) {
setState(() {
snapshot = datasnapshot.documents;
});
});
}
答案 0 :(得分:0)
要在firebase中获取currentuser ID,您需要firebase_auth,然后可以按如下方式获取currentuserId的值
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
final CurrentUserId = user.uid;
//then
Firestore.instance.collection('posts').document(CurrentUserId).collection('usersPosts');