首页 > 解决方案 > 类'String'没有实例方法'map'

问题描述

我正在使用颤振轮播代码并尝试从 firebase 添加图像。这是我的代码:

class FullscreenSliderDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFF030164),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance.collection('quotes').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return Text('Loading data... Please Wait');

          final double height = MediaQuery.of(context).size.height;
          return Container(
              height: 100.0,
              padding: const EdgeInsets.all(4.0),
              child: new ListView(
                  scrollDirection: Axis.horizontal,
                  children: snapshot.data.documents
                      .map<Widget>((DocumentSnapshot document) {
                    return CarouselSlider(
                        options: CarouselOptions(
                          height: height,
                          viewportFraction: 1.0,
                          enlargeCenterPage: false,
                        ),
                        items: (document.data()['img']).map(
                          (item) => Container(
                            child: Center(
                                child: Image.network(
                              item,
                              fit: BoxFit.cover,
                              height: height,
                            )),
                          ),
                        ).toList());
                  }).toList(),));
        },
      ),
    );
  }
}

我收到了错误Class 'String' has no instance method 'map'.,我不确定如何解决这个问题。如果有人可以提供帮助,我将不胜感激!

标签: flutterdartgoogle-cloud-firestore

解决方案


我想你正在寻找这样的东西:

  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFF030164),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance.collection('quotes').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return Text('Loading data... Please Wait');

          final double height = MediaQuery.of(context).size.height;
          return CarouselSlider(
            options: CarouselOptions(
              height: height,
              viewportFraction: 1.0,
              enlargeCenterPage: false,
            ),
            items: snapshot.data.documents
              .map<Widget>((document) {
                return Container(
                  child: Center(
                    child: Image.network(
                      document.data()['img'],
                      fit: BoxFit.cover,
                      height: height,
                    )
                  ),
                ),
              ).toList()
            )
          )
        },
      ),
    );
  }

有了这个CarouselSlider,所有文档/图像只有一个,而不是每个文档都有一个单独的。


推荐阅读