Search code examples
firebaseflutterfirebase-realtime-databasestream-builder

Flutter Firebase Realtime Database StreamBuilder update


I'm working on a project where I display data from Firebase Realtime Database. I use StreamBuilder to get data from database. For triggering the StreamBuilder I use "_database.child(widget.latestPath).onValue". But sometimes I want to change the path for get another data, but when I change "widget.latesPath" the StreamBuilder doesn't refresh(Only if I use hot reaload). How can I trigger StreamBuilder to refresh the path for the new database?


Solution

  • DatabaseReference databaseRef;
    
    // initState()
    databaseRef = _database.child(widget.latestPath);
    
    // build
    return StreamBuilder(
        stream: databaseRef.onValue,
        builder: (context, snapshot) {
          // Do something with the data
      },
    );
    
    // When u want to update new path
    setState(() {
     databaseRef = _database.child(widget.newPath);;
    });