ユーザーのプロフィール画像と、投稿の画像が被らないようにしたい。

実現したいこと

前提

Flutter×Firebaseでインスタグラムのようなアプリを開発しています。
ホーム画面で、画像の表示に不具合があり、現在参照
先が間違っていないかなどを調べている状況です。

発生している問題・エラーメッセージ

ユーザーのプロフィール画像を変更した際に、投稿済みの画像も切り替わってしまっていることが判明しています。

該当のソースコード

post_firestore.dart

1import 'package:chat_app/model/post.dart'; 2import 'package:cloud_firestore/cloud_firestore.dart'; 3import 'package:flutter/foundation.dart'; 4 5class PostFirestore { 6 static final _firestoreInstance = FirebaseFirestore.instance; 7 static final CollectionReference posts = 8 _firestoreInstance.collection('posts'); 9 10 static Future<dynamic> addPost(Post newPost) async { 11 try { 12 final CollectionReference userPosts = _firestoreInstance 13 .collection('user') 14 .doc(newPost.postAccountId) 15 .collection('my_posts'); 16 var result = await posts.add({ 17 'content': newPost.content, 18 'post_account_id': newPost.postAccountId, 19 'image_path': newPost.imagePath, 20 'post_time': Timestamp.now(), 21 }); 22 userPosts.doc(result.id).set({ 23 'post_id': result.id, 24 'post_time': Timestamp.now(), 25 }); 26 if (kDebugMode) { 27 print("Submission Completed."); 28 } 29 return true; 30 } on FirebaseException catch (e) { 31 if (kDebugMode) { 32 print("Submission Failure. $e"); 33 } 34 return false; 35 } 36 } 37 38 static Future<List<Post>?> getPostsFromIds(List<String> ids) async { 39 List<Post> postList = []; 40 try { 41 await Future.forEach(ids, (String elements) async { 42 var doc = await posts.doc(elements).get(); 43 Map<String, dynamic> data = doc.data() as Map<String, dynamic>; 44 Post post = Post( 45 id: doc.id, 46 imagePath: data['image_path'], 47 content: data['content'], 48 postAccountId: data['post_account_id'], 49 postTime: data['post_time'], 50 ); 51 postList.add(post); 52 }); 53 if (kDebugMode) { 54 print("Success in retrieving my post."); 55 print("postList => $postList"); 56 } 57 return postList; 58 } on FirebaseException catch (e) { 59 if (kDebugMode) { 60 print("Failure to retrieve my post. $e"); 61 } 62 return null; 63 } 64 } 65 66 static Future<dynamic> deletePosts(String accountId) async { 67 final CollectionReference userPosts = _firestoreInstance 68 .collection('user') 69 .doc(accountId) 70 .collection('my_posts'); 71 var snapshot = await userPosts.get(); 72 for (var doc in snapshot.docs) { 73 await posts.doc(doc.id).delete(); 74 userPosts.doc(doc.id).delete(); 75 } 76 } 77} 78 79

試したこと

home_page.dartの29行目から57行目のあたりで参照先が間違えていることが可能性として考えられる状況なので、周辺のファイルなどCloud Firestoreも含めたところで見直し中ですがStreambuilderを用いて開発しているので、Streambuilderの実装の方法なども見直しているところです。
ホームページのコードは長くなるのでコメント欄に記載させていただきます。

補足情報(FW/ツールのバージョンなど)

[✓] Flutter (Channel stable, 3.10.5, on macOS 13.4 22F66 darwin-x64,
locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version
32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.79.2)
[✓] Connected device (3 available)
[✓] Network resources

コメントを投稿

0 コメント