Firebase 如何从StreamProvider中访问数据

Firebase 如何从StreamProvider中访问数据,firebase,flutter,dart,google-cloud-firestore,flutter-provider,Firebase,Flutter,Dart,Google Cloud Firestore,Flutter Provider,我正在尝试显示一个提醒列表,用户可以在其中查看所有的提醒并进行编辑。我正在使用StreamProvider,以便能够在子窗口小部件中访问。我将DocumentSnapShot映射到MedReminder对象。StreamProvider应返回对象列表。但是在提醒列表小部件中,提供者返回null 流: List<MedReminder> _medReminderListFromSnapshot(QuerySnapshot snapshot) { print('here is

我正在尝试显示一个提醒列表,用户可以在其中查看所有的提醒并进行编辑。我正在使用StreamProvider,以便能够在子窗口小部件中访问。我将DocumentSnapShot映射到MedReminder对象。StreamProvider应返回对象列表。但是在提醒列表小部件中,提供者返回null

流:

  List<MedReminder> _medReminderListFromSnapshot(QuerySnapshot snapshot) {
    print('here is db');
    return snapshot.documents.map((doc) {
      return MedReminder(
        remindId: doc.data['remindId'] ?? '',
        howManyTimeDay: doc.data['howManyTimeDay'] ?? '',
        frequency: doc.data['frequecy'] ?? '',
        hour: doc.data['hour'] ?? '',
        min: doc.data['min'] ?? '',
        dateStarted: doc.data['dateStarted'] ?? '',
        dateEnded: doc.data['dateEnded'] ?? '',
        dateRefill: doc.data['dateRefill'] ?? '',
        quantityTime: doc.data['quantityTime'] ?? '',
      );
    }).toList();
  }
  Stream<List<MedReminder>> get medReminders {
    return userCollection.document(uid).collection('medic_reminder').snapshots()
        .map(_medReminderListFromSnapshot);
  }
List\u医疗提醒列表fromsnapshot(QuerySnapshot快照){
打印(“这里是db”);
返回snapshot.documents.map((doc){
退货提醒(
提醒ID:doc.data['memendId']??“,
howManyTimeDay:文档数据['howManyTimeDay']??“,
频率:文件数据[“频率”]??“,
小时:文件数据['hour']??“,
最小值:文档数据['min']??“,
dateStarted:doc.data['dateStarted']??“,
dateEnded:文档数据['dateEnded']??“,
dateRefill:doc.data['dateRefill']??“,
quantityTime:文档数据['quantityTime']??“,
);
}).toList();
}
流获取提醒{
返回userCollection.document(uid.collection('medic_提醒')。快照()
.map(_MedEmleristfromSnapshot);
}
提醒页面:

Widget build(BuildContext context) {
    final user = Provider.of<User>(context);
    return StreamProvider<List<MedReminder>>.value(
      value: DatabaseService(uid: user.uid).medReminders,
      child: Scaffold(
        body: Container(
          child: ReminderList(),
        ),
      ),
    );
  }
小部件构建(构建上下文){
最终用户=提供者(上下文);
返回StreamProvider.value(
值:DatabaseService(uid:user.uid).MED提醒,
孩子:脚手架(
主体:容器(
子项:提醒列表(),
),
),
);
}
提醒列表小部件(此处提供程序返回null):

小部件构建(构建上下文){
最终提醒=提供者(上下文)??[];
提醒。forEach((提醒){
打印(提醒。提醒ID);
});
返回ListView.builder(
itemCount:提醒。长度,
itemBuilder:(上下文,索引){
返回提醒();
});
}
请帮帮我,谢谢

Stream<List<MedReminder>> get medReminders {
    return userCollection.snapshots()
        .map(_medReminderListFromSnapshot);
  }
在“用户”中保存数据时

userCollection.document(uid).setData()..

然后它就开始工作了

你的流是否有emmiting值?在返回snapshot.documents.map之前,您确定它不是空的吗?我尝试打印snapshot.documents.map内的_MedRememberListFromSnapshot它显示我有文档snapshot的实例然后它不是空的,但是当我尝试通过流提供程序访问小部件子部件中的提醒对象列表时,列表为空。请帮我修好了吗?如果是的话,请张贴一个答案。
collectionReference userCollection = Firestore.instance.collection('users');
userCollection.document(uid).setData()..