0

I have two widgets: Home and EntryList. In the Home widget, I can change the value date that is always being passed into EntryList which should then update it's Stream widget.

However, when I change the Date value, the EntryList widget is still displaying items from the initial date instead of this new date

Home

class HomePage extends StatefulWidget {
  const HomePage();
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  DateTime date = new DateTime.now();
  void changeDate(int num) {
    var newDate =
        new DateTime(this.date.year, this.date.month, this.date.day + num);
    setState(() {
      date = newDate;
    });
  }
   @override
   Widget build(BuildContext context) {
   
    return Scaffold(
        body: Column(
         children: [              
                // Change Date and display Date here
                Row(
                  children: [
                       ElevatedButton(
                          onPressed: () {
                            changeDate(-1);
                       }),
                      Text(date.toString()),
                      ElevatedButton(
                          onPressed: () {
                            changeDate(1);
                       }),
                        
                   ]
                ),
                // Our main child component EntryList and passing in date
                Row(children: [EntryList(this.date)]),
         ]
       )
    )
   }
}

EntryList

class EntryList extends StatefulWidget {
  final DateTime date;
  const EntryList(this.date);
  @override
  _EntryListState createState() => _EntryListState();
}

class _EntryListState extends State<EntriesList> {
  Stream<QuerySnapshot> _entryStream; // my stream

  void getEntries(snapshot) {
     // code to get items - this works just fine
  }

  @override
  // This is where we use the props in this widget. Not updating after changing parent date value
  void initState() {
    _entryStream = FirebaseFirestore.instance
        .collection(widget.date)
        .snapshots();
    super.initState();
   }
   Widget build(BuildContext context) {
       return (StreamBuilder(
       stream: _entryStream,
       builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasError) {
          return Text("Something went wrong");
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        }
        return new ListView(
          children: getEntries(snapshot),
        );,
      },
    ));
  
       
   }
}

Can anyone tell me why its not updating? I think that this may have to do with using void dispose but i'm unsure how to use it in this context.

Related (but not my solution) links: How to Set/Update State of StatefulWidget from other StatefulWidget in Flutter?

pythonNovice
  • 1,130
  • 1
  • 14
  • 36

1 Answers1

1

I discovered a function called didUpdateWidget which allows me to update my widget when the parent value changes.

Here is the answer that helped me: Flutter: re-render child widget on parent state change

  @override
  void didUpdateWidget(old) {
    super.didUpdateWidget(old);
    String dateString =
        '${widget.date.month}-${widget.date.day}-${widget.date.year}';
    _entryStream = FirebaseFirestore.instance
        .collection('users')
        .doc(FirebaseAuth.instance.currentUser.uid)
        .collection(dateString)
        .snapshots();
  }

Now my child widget correctly shows the new data once the date changes.

pythonNovice
  • 1,130
  • 1
  • 14
  • 36