【问题标题】:Retrieve list<string> from Firestore into flutter page从 Firestore 中检索 list<string> 到颤动页面
【发布时间】:2020-04-18 15:08:11
【问题描述】:

我想将详细信息显示在我的 Cloud Firestore 中显示的图像中,以显示电影颤振页面,但不知道如何从 Firestore 中检索列表以及如何在代码中实现的自定义卡片中显示该列表。 我尝试了其他方法但没有成功。 有人可以帮助实施上述内容吗? 这是我的第一个问题,非常抱歉出现错误。

    class CustomCard extends StatelessWidget {
      CustomCard(
          {@required this.movie_name,
          this.genre,
          this.famous_cast,
          this.rating,
          this.comment,
          this.free_link});

      final movie_name;
      final genre;
      final famous_cast;
      final rating;
      final comment;
      final free_link;

      @override
      Widget build(BuildContext context) {
        return Card(
          child: Container(
            padding: const EdgeInsets.symmetric(vertical: 15.0, horizontal: 10.0),
            child: Column(
              children: <Widget>[
                Text(movie_name),
                Text(genre),
                Text(famous_cast),
                Text(rating),
                Text(comment),
                Text(free_link),
              ],
            ),
          ),
        );
      }
    }

    class ShowPage extends StatefulWidget {
      @override
      _ShowPageState createState() => _ShowPageState();
    }

    class _ShowPageState extends State<ShowPage> {
      final db = Firestore.instance.collection('Movies');

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          resizeToAvoidBottomPadding: false,
          appBar: AppBar(
            title: Text('Show Movies'),
            backgroundColor: Colors.blueGrey[900],
          ),
          backgroundColor: Colors.white,
          body: Container(
            padding: const EdgeInsets.all(20.0),
            child: StreamBuilder<QuerySnapshot>(
              stream: db.snapshots(),
              builder:
                  (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
                switch (snapshot.connectionState) {
                  case ConnectionState.waiting:
                    return new Text('Loading.....');
                  default:
                    return new ListView(children: getExpenseItems(snapshot));
                }
              },
            ),
          ),
        );
      }

      getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot) {
        return snapshot.data.documents
            .map(
              (doc) => new CustomCard(
                movie_name: new Text(doc["movie_n"]),
                genre: new Text(doc["genre"]),
                famous_cast: new Text(doc["famous_c"]),
                rating: new Text(doc["rating"].toString()),
                comment: new Text(doc["comments"]),
                free_link: new Text(doc["free_link"]),
              ),
            )
            .toList();
      }
    }

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    由于我不确定CustomCard 的期望是什么,我假设它是List&lt;Widget&gt;,所以你会想做这样的事情。

    class CustomCard extends StatelessWidget {
      CustomCard(
          {@required this.movie_name,
          this.genres,
          this.famous_casts,
          this.rating,
          this.comment,
          this.free_link});
    
      final movie_name;
      final List<Object> genres;
      final List<Object> famous_casts;
      final rating;
      final comment;
      final free_link;
    
      @override
      Widget build(BuildContext context) {
       return Card(
          child: Container(
            padding: const EdgeInsets.symmetric(vertical: 15.0, horizontal: 10.0),
            child: Column(
              children: <Widget>[
                Text(movie_name),
                ...genres
                    .map(
                      (genre) => Text(genre + ', '),
                    )
                    .toList(),
                ...famous_casts
                    .map(
                      (famous_cast) => Text(famous_cast + ', '),
                    )
                    .toList(),
                Text(rating),
                Text(comment),
                Text(free_link),
              ],
            ),
          ),
        );
      }
    }
    
    getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot) {
            return snapshot.data.documents
                .map(
                  (doc) =>  CustomCard(
                    movie_name:  doc["movie_n"],
                    genres:  doc["genre"],
                    famous_casts:  doc["famous_c"],
                    rating:  doc["rating"].toString(),
                    comment: doc["comments"],
                    free_link:  doc["free_link"],
                  ),
                )
                .toList();
          }
    

    试试这个

    【讨论】:

    • 我想在卡片中显示完整的数据。我需要帮助,比如如何将作为列表的类型传递到我的自定义卡中并将其放入 Text()。
    • 您能告诉我如何将您从数据库中检索到的类型传递给我的自定义卡片,以及如何显示它吗?
    • 试试这些改变
    • 一个疑问:你编辑的代码中的那三个点是什么?
    • 出现错误:类型转换中的“String”类型不是“List”类型的子类型。
    猜你喜欢
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2020-12-05
    • 2020-09-15
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多