Firebase 颤振中如何将函数内部的数据传递到不同的类
作为一名Flutter初学者,我正在尝试构建一个日历应用程序,在该应用程序中,我希望从Firestore数据库中获取特定日期的数据。现在有一个_onDaySelected函数,它在颤振控制台中从日历中打印所选日期,如“颤振:2019-11-04”。我希望将此字符串传递到MessagesStream类,以便调用.document(“$currentDay”),而不是像这样硬编码日期:.document('2019-11-04')。我已经包括了下面的代码 有人知道怎么做吗?任何帮助都将不胜感激Firebase 颤振中如何将函数内部的数据传递到不同的类,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,作为一名Flutter初学者,我正在尝试构建一个日历应用程序,在该应用程序中,我希望从Firestore数据库中获取特定日期的数据。现在有一个_onDaySelected函数,它在颤振控制台中从日历中打印所选日期,如“颤振:2019-11-04”。我希望将此字符串传递到MessagesStream类,以便调用.document(“$currentDay”),而不是像这样硬编码日期:.document('2019-11-04')。我已经包括了下面的代码 有人知道怎么做吗?任何帮助都将不胜感激 cl
class RoosterTest extends StatefulWidget {
@override
_RoosterTestState createState() => _RoosterTestState();
}
class _RoosterTestState extends State<RoosterTest>
with TickerProviderStateMixin {
Map<DateTime, List> _events;
List _selectedEvents;
AnimationController _animationController;
CalendarController _calendarController;
final messageTextController = TextEditingController();
String messageText;
static final now = DateTime.now();
static final formatter = DateFormat('yyyy-MM-dd');
static final formatted = formatter.format(now);
@override
void initState() {
super.initState();
final _selectedDay = DateTime.now();
_events = {};
_selectedEvents = _events[_selectedDay] ?? [];
_calendarController = CalendarController();
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 400),
);
_animationController.forward();
}
@override
void dispose() {
_animationController.dispose();
_calendarController.dispose();
super.dispose();
}
void _onDaySelected(DateTime day, List events) {
String currentDay = formatter.format(day).toString();
print('$currentDay');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
_buildTableCalendar(),
const SizedBox(
height: 8.0,
),
MessagesStream(),
],
),
);
}
Widget _buildTableCalendar() {
return TableCalendar(
calendarController: _calendarController,
events: _events,
startingDayOfWeek: StartingDayOfWeek.monday,
calendarStyle: CalendarStyle(
selectedColor: Colors.red,
todayColor: Colors.blue,
markersColor: Colors.green,
outsideDaysVisible: false,
weekendStyle: TextStyle().copyWith(color: Colors.red),
),
headerStyle: HeaderStyle(
formatButtonTextStyle:
TextStyle().copyWith(color: Colors.white, fontSize: 15.0),
formatButtonDecoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(16.0),
),
),
onDaySelected: _onDaySelected,
onVisibleDaysChanged: _onVisibleDaysChanged,
);
}
class RoosterTest扩展了StatefulWidget{
@凌驾
_RoosterTestState createState();
}
类RoosterTestState扩展了状态
使用TickerProviderStateMixin{
地图事件;
列出所选事件;
AnimationController _AnimationController;
CalendarController\u CalendarController;
最终消息TextController=TextEditingController();
字符串消息文本;
静态final now=DateTime.now();
静态最终格式化程序=日期格式('yyyy-MM-dd');
静态最终格式化=formatter.format(现在);
@凌驾
void initState(){
super.initState();
final _selectedDay=DateTime.now();
_事件={};
_selectedEvents=_事件[_selectedDay]??[];
_calendarController=calendarController();
_animationController=animationController(
vsync:这个,,
持续时间:常量持续时间(毫秒:400),
);
_animationController.forward();
}
@凌驾
无效处置(){
_animationController.dispose();
_calendarController.dispose();
super.dispose();
}
void\u onDaySelected(日期时间日,列出事件){
字符串currentDay=formatter.format(day.toString();
打印(“$currentDay”);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
mainAxisSize:mainAxisSize.max,
儿童:[
_buildTableCalendar(),
常量大小框(
身高:8.0,
),
MessagesStream(),
],
),
);
}
小部件_buildTableCalendar(){
返回表日历(
calendarController:\u calendarController,
事件:_事件,
星期一开始:星期一开始,
calendarStyle:calendarStyle(
selectedColor:Colors.red,
今天的颜色:颜色。蓝色,
markersColor:Colors.green,
outsideDaysVisible:错误,
weekendStyle:TextStyle().copyWith(颜色:Colors.red),
),
headerStyle:headerStyle(
formatButtonTextStyle:
TextStyle().copyWith(颜色:Colors.white,fontSize:15.0),
FormatButton装饰:盒子装饰(
颜色:颜色,红色,
边界半径:边界半径。圆形(16.0),
),
),
onDaySelected:_onDaySelected,
onVisibleDaysChanged:\u onVisibleDaysChanged,
);
}
类消息流扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回流生成器(
流:_firestore
.收款(天)
.文件('2019-11-04')
.收款(小时)
.snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData)
返回循环预测器(
背景颜色:Colors.lightBlueAccent,
);
返回构建列表(上下文、快照、数据、文档);
},
);
}
您可以将\on dayselected
方法的结果存储在state类的变量中:
class\u RoosterTestState扩展状态
使用TickerProviderStateMixin{
字符串stringOfSelectedDay='defaultValue';
[...]
别忘了在第一次打开应用程序时,您应该为stringOfSelectedDay
提供一个默认值。当尚未调用所选日期时,您的消息流应该返回什么?可能是今天的日期
因此,在方法上,您可以将其存储为:
void\u onDaySelected(日期时间日,列出事件){
字符串currentDay=formatter.format(day.toString();
设置状态((){
stringOfSelectedDay=当前日期;
});
打印(“$currentDay”);
}
您必须使用setState使消息流在日期更改后重新生成自身
在此之后,必须使MessagesStream类具有最终变量,使其基于该变量构建流,并具有赋予其值的构造函数:
class MessagesStream extends StatelessWidget {
final String date;
MessagesStream(this.date); //Constructor
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('days')
.document(date) // Using the final variable here
.collection('hours')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
);
return _buildList(context, snapshot.data.documents);
},
);
}
class MessagesStream extends StatelessWidget {
final String date;
MessagesStream(this.date); //Constructor
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('days')
.document(date) // Using the final variable here
.collection('hours')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
);
return _buildList(context, snapshot.data.documents);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
_buildTableCalendar(),
const SizedBox(
height: 8.0,
),
MessagesStream(stringOfSelectedDay), //Using the new constructor you've made.
],
),
);
}