Return Type of Timestamp from Firestore and comparing to DateTime.now() in Flutter

5,553
DateTime _now = DateTime.now();
DateTime _start = DateTime(_now.year, _now.month, _now.day, 0, 0);
DateTime _end = DateTime(_now.year, _now.month, _now.day, 23, 59, 59);


stream: Firestore.instance
           .collection('schedule')
           .where('date', isGreaterThanOrEqualTo: _start) 
           .where('date', isLessThanOrEqualTo: _end) 
           .orderBy('date')
           .snapshots(),
Share:
5,553
hefty_kat
Author by

hefty_kat

Just help... don't berate people

Updated on December 08, 2022

Comments

  • hefty_kat
    hefty_kat over 1 year

    **EDIT --------->

    Additional info. It is possible that I am going about it all wrong. I am trying to achieve the image below. Is an infinite scrolling "calendar" that shows a feed for the corresponding day. I'm beginning to think this is not a tab view issue and it needs to be done some other way.

    enter image description here

    ------------------>

    I'm creating an app that creates a StreamBuilder in Flutter that relies on comparing a date to return Firestore data.

    I've created an array of possible date tab values. When the user selects that tab I need to compare the DATE ONLY - no timestamp and show a stream of games that have a matching date field in local time.

    I have multiple tabs with one changing view so it isn't a 1:1 ratio of tabs to widgets but the tab needs to control the state of the widget by returning the date value at a clicked index and setting that to stateDate.

    Also it would be great to start the user selected on the tab that is todays date.

    var datesList = [];
    var _stateDate = DateTime.now();
    
    initState(){
      for (int i = -14; i < 57; i++) {
        DateTime date = _stateDate;
        date = date.add(Duration(days: i));
        datesList.add(date);
      }
    }
    

    ------------------------------------------------------>

    @override
    Widget build(BuildContext context) {
      return DefaultTabController(
        length: dateList.length,
      child: Scaffold(
        appBar: AppBar(
          bottom: const PreferredSize(
            preferredSize: Size.zero,
            child: TabBar(
              tabs: <Widget>[
                _buildTabs(),
              ],
              isScrollable: true,
            ),
          ),
        ),
        body: TabBarView(
          children: <Widget>[
            _buildSchedule()
          ],
        ),
      ),
     );
    }
    
    
    @override
      Widget _buildSchedule() {
        return Container(
          child: Center(
            child: StreamBuilder<QuerySnapshot>(
                stream: Firestore.instance
                   .collection('schedule')
                   .where('time', isEqualTo: _stateDate) 
                   .orderBy('date')
                   .snapshots(),
                builder:
                    (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                  if (!snapshot.hasData) {
                    return const Text('Loading...');
                  }
    
              return ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (BuildContext context, int index) =>
                    _buildScheduleItem(context, snapshot.data.documents[index]),
              );
            }),
          ),
        );
       }
    

    enter image description here

    Any help would be great. Maybe I am just thinking about it all wrong.

  • hefty_kat
    hefty_kat over 5 years
    oh this makes sense. Just sandwich the time of the current day. I'll give this a try! Thank you!
  • hefty_kat
    hefty_kat over 5 years
    you have any idea how I might implement this with a horizontal scroll view in the image above?
  • blaneyneil
    blaneyneil over 5 years
    from your streambuilder call 'builder: (context, snapshot) {" and then inside of there call a ListView.builder with scrollDirection: Axis.horizontal and use snapshot.data.documents as your source