Flutter・Firestore初心者です。
Firestoreからデータを取るため、下記のように「投稿リスト」を取って来ようとしています。
しかし、for文の中の非同期処理が上手くいっていないのか、ずっとreturnされないまま止まってしまいます。
Firestoreにドキュメントのリファレンスを格納しており、そのリファレンスデータを取ってくるのに、for文を使用しています。
dart
1 // ここのリストを取ってきて表示させたい!! 2 Future<List<Post>> get list async { 3 return usersInfoCollection 4 .document(uid) 5 .collection('likedPosts') 6 .orderBy('createdAt', descending: true) 7 .limit(10) 8 .getDocuments() 9 .then(_userPostListFromSnapshot) 10 .catchError((Exception e) => print('userLikedList error:$e')); 11 } 12 13 Future<List<Post>> _listFromSnapshot(QuerySnapshot collection) async { 14 final posts = <Post>[]; 15 try { 16 // ここのfor文が非同期処理でうまくいっていない?? 17 for (final document in collection.documents) { 18 final ref = db.document(document['postRef'].toString()); 19 await ref.get().then((docPost) { 20 final post = Post( 21 nickname: docPost['nickname'] as String ?? '', 22 createdAt: docPost['createdAt'] as Timestamp, 23 updatedAt: docPost['updatedAt'] as Timestamp, 24 postId: docPost.documentID, 25 ); 26 posts.add(post); 27 }).catchError((Exception e) { 28 Fluttertoast.showToast(msg: '投稿取得中にエラーが発生しました。'); 29 print('Error liked_post_api._userPostListFromSnapshot :$e'); 30 }); 31 } 32 } on Exception catch (e) { 33 print('futurelist error:$e'); 34 } 35 return posts; 36 }
なぜか、この「list」を使用する画面でホットリロードを行うと、上手く表示され、別の画面でホットリロードして「list」を使用する画面に遷移すると失敗してしまいます。。
エラーは下記のものが出力されます。
E/flutter (26375): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument (onError): Error handler must accept one Object or one Object and a StackTrace as arguments, and return a a valid result: Closure: (Exception) => Null E/flutter (26375): #0 _registerErrorHandler (dart:async/future_impl.dart:809:3) E/flutter (26375): #1 Future.catchError (dart:async/future_impl.dart:304:17) E/flutter (26375): #2 LikedPostApi._awaitPostListGet (package:scrum/models/api/liked_post_api.dart:40:12) E/flutter (26375): #3 _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:45:6) E/flutter (26375): #4 LikedPostApi._awaitPostListGet (package:scrum/models/api/liked_post_api.dart:17:39) E/flutter (26375): #5 LikedPostApi._userPostListFromSnapshot (package:scrum/models/api/liked_post_api.dart:14:12) E/flutter (26375): #6 _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:45:6) E/flutter (26375): #7 LikedPostApi._userPostListFromSnapshot (package:scrum/models/api/liked_post_api.dart:13:47) E/flutter (26375): #8 _rootRunUnary (dart:async/zone.dart:1134:38) E/flutter (26375): #9 _CustomZone.runUnary (dart:async/zone.dart:1031:19) E/flutter (26375): #10 _FutureListener.handleValue (dart:async/future_impl.dart:139:18) E/flutter (26375): #11 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45) E/flutter (26375): #12 Future._propagateToListeners (dart:async/future_impl.dart:709:32) E/flutter (26375): #13 Future._completeWithValue (dart:async/future_impl.dart:524:5) E/flutter (26375): #14 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:32:15) E/flutter (26375): #15 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:290:13) E/flutter (26375): #16 Query.getDocuments (package:cloud_firestore/src/query.dart) E/flutter (26375): #17 _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:73:64) E/flutter (26375): #18 _rootRunUnary (dart:async/zone.dart:1134:38) E/flutter (26375): #19 _CustomZone.runUnary (dart:async/zone.dart:1031:19) E/flutter (26375): #20 _FutureListener.handleValue (dart:async/future_impl.dart:139:18) E/flutter (26375): #21 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45) E/flutter (26375): #22 Future._propagateToListeners (dart:async/future_impl.dart:709:32) E/flutter (26375): #23 Future._completeWithValue (dart:async/future_impl.dart:524:5) E/flutter (26375): #24 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:32:15) E/flutter (26375): #25 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:290:13) E/flutter (26375): #26 MethodChannelQuery.getDocuments (package:cloud_firestore_platform_interface/src/method_channel/method_channel_query.dart) E/flutter (26375): #27 _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:73:64) E/flutter (26375): #28 _rootRunUnary (dart:async/zone.dart:1134:38) E/flutter (26375): #29 _CustomZone.runUnary (dart:async/zone.dart:1031:19) E/flutter (26375): #30 _FutureListener.handleValue (dart:async/future_impl.dart:139:18) E/flutter (26375): #31 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45) E/flutter (26375): #32 Future._propagateToListeners (dart:async/future_impl.dart:709:32) E/flutter (26375): #33 Future._completeWithValue (dart:async/future_impl.dart:524:5) E/flutter (26375): #34 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:32:15) E/flutter (26375): #35 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:290:13) E/flutter (26375): #36 MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart) E/flutter (26375): #37 _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:73:64) E/flutter (26375): #38 _rootRunUnary (dart:async/zone.dart:1134:38) E/flutter (26375): #39 _CustomZone.runUnary (dart:async/zone.dart:1031:19) E/flutter (26375): #40 _FutureListener.handleValue (dart:async/future_impl.dart:139:18) E/flutter (26375): #41 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45) E/flutter (26375): #42 Future._propagateToListeners (dart:async/future_impl.dart:709:32) E/flutter (26375): #43 Future._completeWithValue (dart:async/future_impl.dart:524:5) E/flutter (26375): #44 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:32:15) E/flutter (26375): #45 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:290:13) E/flutter (26375): #46 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart) E/flutter (26375): #47 _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:73:64) E/flutter (26375): #48 _rootRunUnary (dart:async/zone.dart:1134:38) E/flutter (26375): #49 _CustomZone.runUnary (dart:async/
一人で悩み続けたのですが全く解決せず、ご回答いただければとても嬉しいです!!
よろしくお願いします!!