flutter - 类'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'.
,我不确定如何解决这个问题。如果有人可以提供帮助,我将不胜感激!
解决方案
我想你正在寻找这样的东西:
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
,所有文档/图像只有一个,而不是每个文档都有一个单独的。
推荐阅读
- python - Scipy 差分进化初始解输入
- electron - 最小化后电子高度变化,恢复
- python - 或者当使用 Python 存在 NaN 值时逻辑不起作用
- youtube-api - 为什么 YouTube Data API 搜索经常会找到 0 个结果,而普通的 YouTube 网站搜索会产生很多结果?
- android - 错误:在 FirebaseMessagingService 上注入时无法访问 RemoteMessage
- oracle-sqldeveloper - 根据表 1 中字符串内容的条件连接两个表
- python - 如何使用 Pythons Multiprocessing 库的 Process 类多次运行一个函数?
- powerbi - 与前一周相比,选定周的排名和排名增量/减量
- azure-ad-b2c - 自定义策略短信
- python - pandas.core.series.series 到数据框