【问题标题】:Retrieve Map Data in Firebase to Flutter将 Firebase 中的地图数据检索到 Flutter
【发布时间】:2020-07-07 20:55:30
【问题描述】:

我有这个数据结构 Database Structure

我需要调用 FavDestination 集合中的所有项目,这是我在 Flutter 中的代码

child: StreamBuilder(
stream: Firestore.instance.collection('FavDestination').snapshots(),
builder: (context, snapshot){
if(!snapshot.hasData) return Text('Loading Data... Please Wait');
return
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.documents.length, //snapshot.data.document.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot destination = snapshot.data.documents[index];
//DocumentSnapshot destination = snapshot.data.document[index];
return GestureDetector(
                  onTap: () => Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (_) => DestinationScreen(
                        destination: destination,
                      ),
                    ),
                  ),

它成功检索数据,如果用户点击文档,它将转到目标屏幕,我需要检索活动中的所有数据,这是我在目标屏幕中的代码

Expanded(
        child: ListView.builder(
          padding: EdgeInsets.only(top: 10.0, bottom: 15.0),
          itemCount: widget.destination['activities'].length,
          itemBuilder: (BuildContext context, int index) {
            DocumentSnapshot activity = widget.destination['activities'][index];
            return Stack(
              children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                       Container(
                          width: 120.0,
                          child: Text(
                          activity['name'],
                          style: TextStyle(
                          fontSize: 18.0,
                          fontWeight: FontWeight.w600,
                          ),

这是我得到的错误 Error Message

知道如何解决吗?谢谢

【问题讨论】:

  • 错误表明activity['name'] 为空。这意味着您要么没有正确检索数据,要么没有正确处理它们。除非我看到完整的代码,否则我不能说。

标签: firebase flutter google-cloud-firestore


【解决方案1】:

您的activities 字段似乎是嵌套映射。在第一级,您有字段Bali1,它也是地图。并且代码活动试图在第一级获取name

我想你应该更多地研究destination 对象结构。您可以将其打印出来进行分析,并尝试映射 Map&lt;dynamic, dynamic&gt; 和 Bali1 等对象。

我没有操场可以快速完成,但我在这里找到了类似的方法: How do I get specific values from a datasnapshot in flutter?

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 2021-12-07
    • 2021-01-07
    • 2018-10-11
    • 2020-01-15
    • 1970-01-01
    • 2021-10-16
    • 2021-06-05
    • 1970-01-01
    相关资源
    最近更新 更多