【问题标题】:Creating Carousel using FutureBuilder使用 FutureBuilder 创建轮播
【发布时间】:2019-02-08 12:23:55
【问题描述】:

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

     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) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return new CircularProgressIndicator(); 
            } else {
              if (snapshot.hasError) {
                return new Text("fetch error");
              } else {
                return new Container(
                    height: 250.0,
                    child: new Carousel(
                      boxFit: BoxFit.cover,
                      images: [NetworkImage(snapshot.data[0].data["img_2"])],
                      autoplay: false,
                      dotSize: 4.0,
                      indicatorBgPadding: 4.0,
                      animationCurve: Curves.fastOutSlowIn,
                      animationDuration: Duration(milliseconds: 1000),
                    ));
              }
            }
          }),
    );
  }'

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

下面是我的firestore结构:

【问题讨论】:

  • 我需要这个太有趣了...
  • 遇到和你一样的问题。因此,请在解决方案可用时更新此内容。
  • @stucked_overflow,请查看我更新的问题。我让它显示一张图像。我只需要有关显示图像列表的帮助。
  • 这是真正的交易...如果您在其中使用 FutureBuilder 进行迭代,那么它将显示您之前的错误 -> 将 Future 转换为 ImageProvider
  • 正确。这就是我卡住的地方。

标签: dart flutter flutter-layout


【解决方案1】:

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

 Widget build(BuildContext context) {
    var idx = 1;
    return Container(
      child: FutureBuilder(
          future: getCarouselWidget(),
          builder: (context, AsyncSnapshot snapshot) {
            List<NetworkImage> list = new List<NetworkImage>();
            if (snapshot.connectionState == ConnectionState.waiting) {
              return new CircularProgressIndicator(); 
            } else {
              if (snapshot.hasError) {
                return new Text("fetch error");
              } else {

                //Create for loop and store the urls in the list 
                for(int i = 0; i < snapshot.data[0].data.length; i++ ) {
                  debugPrint("Index is " + idx.toString());
                  list.add(NetworkImage(snapshot.data[0].data["img_"+idx.toString()]));
                  idx++;
                }
                return new Container(
                    height: 250.0,
                    child: new Carousel(
                      boxFit: BoxFit.cover,
                      images: list,   <== Set the list here 
                      autoplay: true,
                      dotSize: 4.0,
                      indicatorBgPadding: 4.0,
                      animationCurve: Curves.fastOutSlowIn,
                      animationDuration: Duration(milliseconds: 1000),
                    ));
              }
            }
          }),
    );

【讨论】:

  • 感谢上述示例,它帮助我为我的颤振应用程序做同样的事情。
【解决方案2】:

按照这个 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();
  }

【讨论】:

  • 感谢您的指导。请查看我编辑的答案。
  • 您必须将 getCarouselWidget 更改为图像列表。你要我做转换吗?我没有访问权限,所以我将根据文档编写代码
  • 当然。但是,如果您在小部件中返回图像列表,您将必须有一个构建每个图像的 itembuilder 对吗?
  • 好像轮播为你做了。
  • 好的。如果您能告诉我如何返回图像列表,我将不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2021-10-12
相关资源
最近更新 更多