実現したいこと
- firestoreから投稿データを取得して、プロフィールページに表示させる。
前提
Flutterを用いてインスタグラムのプロフィールページを実装中です。
自分のIDで投稿されたものを自分のプロフィールページに表示させたい。
発生している問題・エラーメッセージ
投稿データが、プロフィールページに表示されない。
該当のソースコード
my_profile_page.dart
1import 'dart:io'; 2 3import 'package:chat_app/firestore/account_firestore.dart'; 4import 'package:chat_app/firestore/post_firestore.dart'; 5import 'package:chat_app/model/account.dart'; 6import 'package:chat_app/model/post.dart'; 7import 'package:chat_app/pages/edit_account_page.dart'; 8import 'package:chat_app/utils/authentication.dart'; 9import 'package:cloud_firestore/cloud_firestore.dart'; 10import 'package:flutter/cupertino.dart'; 11import 'package:flutter/material.dart'; 12 13class MyProfilePage extends StatefulWidget { 14 const MyProfilePage({super.key}); 15 16 @override 17 State<MyProfilePage> createState() => _MyProfilePageState(); 18} 19 20class _MyProfilePageState extends State<MyProfilePage> { 21 Account myAccount = Authentication.myAccount!; 22 File? image; 23 String imagePath = ''; 24 25 @override 26 Widget build(BuildContext context) { 27 return Scaffold( 28 appBar: AppBar( 29 backgroundColor: CupertinoColors.white, 30 elevation: 0, 31 title: const Text( 32 'Profile', 33 style: TextStyle( 34 color: CupertinoColors.black, 35 fontSize: 20.0, 36 fontWeight: FontWeight.bold, 37 ), 38 ), 39 actions: [ 40 IconButton( 41 icon: const Icon(Icons.more_horiz), 42 color: CupertinoColors.systemGrey, 43 onPressed: () {}, 44 ), 45 ], 46 ), 47 body: ListView( 48 children: [ 49 Stack( 50 children: [ 51 Container( 52 height: 200.0, 53 decoration: const BoxDecoration( 54 image: DecorationImage( 55 image: NetworkImage( 56 'https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg', 57 ), 58 fit: BoxFit.cover, 59 ), 60 ), 61 ), 62 Padding( 63 padding: const EdgeInsets.fromLTRB(16.0, 140.0, 16.0, 0), 64 child: Column( 65 crossAxisAlignment: CrossAxisAlignment.start, 66 children: [ 67 Row( 68 mainAxisAlignment: MainAxisAlignment.spaceBetween, 69 children: [ 70 CircleAvatar( 71 radius: 50.0, 72 backgroundColor: CupertinoColors.white, 73 backgroundImage: 74 NetworkImage(myAccount.profileImagePath), 75 child: Container( 76 padding: const EdgeInsets.all(2), 77 decoration: BoxDecoration( 78 color: CupertinoColors.white, 79 shape: BoxShape.circle, 80 border: Border.all( 81 color: CupertinoColors.white, 82 width: 2, 83 ), 84 ), 85 child: CircleAvatar( 86 radius: 48.0, 87 backgroundImage: 88 NetworkImage(myAccount.profileImagePath), 89 ), 90 ), 91 ), 92 Padding( 93 padding: const EdgeInsets.only(top: 80.0), 94 child: ElevatedButton( 95 onPressed: () async { 96 var result = await Navigator.push( 97 context, 98 MaterialPageRoute( 99 builder: (context) => const EditAccountPage(), 100 ), 101 ); 102 if (result == true) { 103 setState( 104 () { 105 myAccount = Authentication.myAccount!; 106 }, 107 ); 108 } 109 }, 110 child: const Text( 111 "Edit", 112 style: TextStyle( 113 color: CupertinoColors.black, 114 fontSize: 16.0, 115 fontWeight: FontWeight.bold, 116 ), 117 ), 118 ), 119 ), 120 ], 121 ), 122 const SizedBox(height: 10.0), 123 Text( 124 myAccount.name, 125 style: const TextStyle( 126 fontSize: 24.0, 127 fontWeight: FontWeight.bold, 128 ), 129 ), 130 const SizedBox(height: 8.0), 131 Text( 132 myAccount.userId, 133 style: const TextStyle( 134 color: CupertinoColors.systemGrey, 135 fontSize: 16.0, 136 ), 137 ), 138 const SizedBox(height: 16.0), 139 const Text( 140 'About', 141 style: TextStyle( 142 fontSize: 20.0, 143 fontWeight: FontWeight.bold, 144 ), 145 ), 146 const SizedBox(height: 8.0), 147 Text( 148 myAccount.selfIntroduction, 149 maxLines: 5, 150 style: const TextStyle( 151 fontSize: 16.0, 152 ), 153 ), 154 const SizedBox( 155 height: 30.0, 156 ), 157 StreamBuilder<QuerySnapshot>( 158 stream: AccountFirestore.account 159 .doc(myAccount.id) 160 .collection('my_posts') 161 .orderBy('created_time', descending: true) 162 .snapshots(), 163 builder: (context, snapshot) { 164 if (snapshot.hasData) { 165 List<String> myPostIds = List.generate( 166 snapshot.data!.docs.length, (index) { 167 return snapshot.data!.docs[index].id; 168 }); 169 return FutureBuilder<List<Post>?>( 170 future: PostFirestore.getPostsFromIds(myPostIds), 171 builder: (context, snapshot) { 172 if (snapshot.hasData && snapshot.data != null) { 173 return GridView.builder( 174 shrinkWrap: true, 175 physics: const ClampingScrollPhysics(), 176 itemCount: snapshot.data!.length, 177 gridDelegate: 178 const SliverGridDelegateWithFixedCrossAxisCount( 179 crossAxisCount: 3, 180 ), 181 itemBuilder: 182 (BuildContext context, int index) { 183 Post post = snapshot.data![index]; 184 return Container( 185 margin: const EdgeInsets.all(2.0), 186 decoration: BoxDecoration( 187 image: DecorationImage( 188 image: 189 NetworkImage(post.postImagePath!), 190 fit: BoxFit.cover, 191 ), 192 borderRadius: 193 BorderRadius.circular(10.0), 194 ), 195 ); 196 }, 197 ); 198 } else { 199 return Container(); 200 } 201 }, 202 ); 203 } else { 204 return Container(); 205 } 206 }, 207 ), 208 ], 209 ), 210 ), 211 ], 212 ), 213 ], 214 ), 215 ); 216 } 217} 218
試したこと
https://blog.flutteruniv.com/flutter-future-stream-difference/
https://www.udemy.com/share/106cqe3@YmTvO6kyRyaTQEk8JRy-teqciAt11VounptSR0uY7OmwscJmwc-YccP0ZDNtYddGwg==/
上記のページを参照して開発をしています。
考えていること
my_profile_page.dartの157行目から、162行目の部分が間違っているのではと思い、現在調査中なのですが、理解が難しい部分でもあり、苦心しています。有識者の方々にアドバイスをいただければ嬉しいです。
補足情報(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
理想形

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。