使用FutureBuilder创建轮播

时间:2019-02-08 12:23:55

标签: dart flutter flutter-layout

我将图像存储在Firestore文档中,并希望使用FutureBuilder加载它们。到目前为止,这是我所做的:

#include<iostream>
#include<algorithm>
#include<vector>
int main()
{
    std::vector<int> x;
    int n;
    std::cin >> n;
    x.resize(n);
    for (auto& e : x) std::cin >> e; 
    auto max = std::max_element(x.begin(), x.end());
    auto min = std::min_element(x.begin(), x.end());
    std::cout << std::distance(max,min) << "\n";
    return 0;
}

使用上面的代码,我可以显示图像而没有任何错误。但是,我不知道如何遍历快照数据以显示图像列表。

下面是我的消防站结构:

My firestore structure

2 个答案:

答案 0 :(得分:0)

遵循此youtube指南 https://www.youtube.com/watch?v=R2I0osLdjgQ https://docs.flutter.io/flutter/widgets/FutureBuilder-class.html

//I assume this function futures a list of images
Future getCarouselWidget() async {
    var firestore = Firestore.instance;
    QuerySnapshot qn = await firestore.collection("carousel").getDocuments();
    return qn.documents;
  }


Widget build(BuildContext context) {
    return Container(
        child: FutureBuilder(
            future: getCarouselWidget(),
            builder: (context, AsyncSnapshot snapshot) {
                switch( snapshot.connectionState ){
                   case ConnectionState.none:
                      return new Text("Data is not fetched");
                   case ConnectionState.waiting:
                      return new CircularProgressIndicator();
                   case ConnectionState.done:
                      if(snapshot.hasError){
                          return new Text("fetch error")
                      } else {
                         return new Container(
                          height: 250.0,
                          child: new Carousel(
                          boxFit: BoxFit.cover,
                          images: snapshot.data,
                          autoplay: false,
                          dotSize: 4.0,
                          indicatorBgPadding: 4.0,
                          animationCurve: Curves.fastOutSlowIn,
                          animationDuration: Duration(milliseconds: 1000),
                        ));
                      }
                   default:
                      return Text("connection is just active");
                }
            }

编辑:我没有关于firestore文档数据类型的文档。 我不知道下面的代码是否正确。

Future getCarouselWidget() async {
    var firestore = Firestore.instance;
    QuerySnapshot qn = await firestore.collection("carousel").getDocuments();
    // convert DocumentSnapshots to Images.
    // I am assuming Document Snapshots are <String, Images>
    var ret = new List();
    qn.documents.forEach( 
       (v) => 
           for( var e in v.data.values ) {
               ret.add(new NetworkImage(e));
           }
     );
    return ret.map((nI) => new Image(image: nI)).toList();
  }

答案 1 :(得分:0)

所以这是解决方案。基本上,我遍历了文档快照并将结果保存在列表中。然后,我将列表设置为图像。

import React from 'react'
import ReactDOM from 'react-dom'
import Home from "./home";

const x:number = "aaa"
const x:number = "aaa"
const x:number = "aaa"

ReactDOM.render(<Home/>, document.getElementById('root'))