1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

mapを使用してListに要素を追加してもうまくいかなかった(QuerySnapshot)

Last updated at Posted at 2021-08-25

QuerySnapshot>にmapを使用してもうまくいかなかった(flutter/dart)

失敗例

import 'package:flutter_riverpod/flutter_riverpod.dart'
class FeedsModel extends ChangeNotifier{
  List<String> bookTitles = [];
  late QuerySnapshot<Map<String, dynamic>> querySnapshots;
  Future BookTitleToList() async {
    querySnapshots = await FirebaseFirestore.collection('books')
    .get
    querySnapshots.docs.map((doc){
      bookTitles.add(doc['bookTitle']);
    });
  }
}

mapとforEachとかどっちも一緒やろ!!と思っていたのが間違いでした...

解決策

querySnapshots.docs.forEach((doc){
  bookTitles.add(doc['bookTitle']);
});

mapをforEachに変えるだけです。

後書き

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?