Flutter:Firebase Streambuilder返回空值

时间:2020-04-29 20:55:38

标签: firebase flutter firebase-realtime-database dart google-cloud-firestore

我在扑朔迷离中从Firestore获取数据时遇到问题。当我运行该应用程序时,它会打印出来(“不好”),这意味着我从Firebase获取了空数据。我仍然不明白。我已经做了一些Google搜索,但是没有帮助。我尝试了几种方法。我认为stream: DatabaseService().schedulesData,的那部分出了问题。

class _DashboardState extends State<Dashboard> {
  @override
  Widget build(BuildContext context) {
    final UserData userData = widget.userData;
    return StreamBuilder<SchedulesData>(
        stream: DatabaseService().schedulesData,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            print('okay');
          } else {
            print('not okay');
          }
          return Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
                  child: Column(
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(top: 30),
                        child: Icon(
                          Icons.add,
                          size: 60,
                          color: Color(0xFF34495e),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          );
        });
  }
}

这是模型类:

class DatabaseService {
 // Schedules data from snapshots
  SchedulesData _schedulesDataFromSnapshot(DocumentSnapshot snapshot) {
    return SchedulesData(
      destination: snapshot.data['destination'],
      active: snapshot.data['active'],
      duration: snapshot.data['duration'],
    );
  }

  Stream<SchedulesData> get schedulesData {
    return Firestore.instance
        .collection('schedules')
        .document('8AAqdgV36T3utTSyLc9d')
        .snapshots()
        .map(_schedulesDataFromSnapshot);
  }
}

在此处查看图片 Heres the image of tha database

0 个答案:

没有答案