title: Changing user preferences in a flutter app when navigating to new screen
body: <p>I have 2 screens in my flutter app and they're defined as follows:</p>
  <ol>
  <li><p><code>allFriends</code>: has a list of users on cards, which when clicked on will bring up a full view of the user (<code>otherUserProfileView</code>).</p>
  </li>
  <li><p><code>otherUserProfileView</code>: shows the profile view (information is loaded from a Firebase Realtime Database)</p>
  </li>
  </ol>
  <p>When I navigate to <code>otherUserProfileView</code>, I still see content about the user that I first viewed after reloading.</p>
  <p>Do you know how I can fix this so I can see a new user each time I navigate to <code>otherUserProfileView</code>, after clicking a new user? Any help is appreciated.</p>
  <p>Below is the code:</p>
  <p><code>allFriends</code>:</p>
  <pre><code>child: InkWell(
          onTap: (){
            Navigator.of(context).push(MaterialPageRoute(
              builder: (context) =&gt;
                  Screen2(
                    friendID: friend.userID,
                    friendName: friend.name,
                    profilePic: friend.picture,
                  ),
            ));
          },
          child: Card()
  )
  </code></pre>
  <p><code>otherUserProfileView</code>:</p>
  <pre><code>class OtherUserProfileView extends StatefulWidget {
    final String friendID;
    final String friendName;
    final String profilePic;
  
    const OtherUserProfileView(
        {Key? key,
          required this.friendID,
          required this.friendName,
          required this.profilePic})
        : super(key: key);
  
    @override
    _OtherUserProfileViewState createState() =&gt; _OtherUserProfileViewState();
  }
  
  class _OtherUserProfileViewState extends State&lt;OtherUserProfileView&gt; {
    List&lt;String&gt; images = [];
    StreamSubscription? _imagesStream;
  
    @override
    void initState() {
      super.initState();
      addImages();
    }
  
    void addImages() {
      images.add(widget.profilePic);
  
      final db = FirebaseDatabase.instance
          .ref()
          .child('users')
          .child(widget.friendID)
          .child(&quot;pictures&quot;);
      _imagesStream = db.onValue.listen((event) {
        if (event.snapshot.exists) {
          final data = new Map&lt;dynamic, dynamic&gt;.from(
              event.snapshot.value as Map&lt;dynamic, dynamic&gt;);
  
          data.forEach((key, value) {
            db.child(key).onValue.listen((event) {
              setState(() {
                images.add(value);
              });
            });
          });
        }
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        extendBodyBehindAppBar: true,
        body: _getContent(),
      );
    }
  
    Widget _getContent() {
      return new ListView(
        scrollDirection: Axis.vertical,
        children: &lt;Widget&gt;[
          CardRow(friendID: widget.friendID),
          images == null
              ? Container()
              : Column(
                children: [
                  Container(
                    height: 200,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      shrinkWrap: true,
                      itemBuilder: (context, index) =&gt;
                          BuildPicture(images[index]),
                      itemCount: images.length,
                    ),
                  ),
                ],
              ),
        ],
      );
    }
  
    @override
    void deactivate() {
      _imagesStream?.cancel();
      super.deactivate();
    }
  }
  
  class BuildPicture extends StatelessWidget {
    final String url;
  
    BuildPicture(this.url);
  
    @override
    Widget build(BuildContext context) {
      return Container(
        height: 200,
        child: Image.network(url),
      );
    }
  }
  
  class CardRow extends StatefulWidget {
    final String friendID;
  
    const CardRow({Key? key, required this.friendID}) : super(key: key);
  
    @override
    State&lt;CardRow&gt; createState() =&gt; _CardRowState();
  }
  
  class _CardRowState extends State&lt;CardRow&gt; {
    late StreamSubscription _userStream;
    late StreamSubscription _friendsStream;
  
    static String uid = &quot;&quot;;
    static DatabaseReference userDatabase =
    FirebaseDatabase.instance.ref().child('users').child(&quot;$uid&quot;);
  
    static List&lt;String&gt; theirFriends = [];
  
    var _userName = &quot;&quot;;
    var _emailAddress = &quot;&quot;;
    var _country = &quot;&quot;;
    var _bio = &quot;&quot;;
    var _profileUrl;
    var user;
    int mutualFriends = theirFriends.length;
  
    @override
    void initState() {
      super.initState();
      uid = widget.friendID;
      _activateListeners();
      _retrieveFriends();
    }
  
    void _activateListeners() {
      _userStream = userDatabase.onValue.listen((event) {
        if (event.snapshot.exists) {
          final data = new Map&lt;dynamic, dynamic&gt;.from(
              event.snapshot.value as Map&lt;dynamic, dynamic&gt;);
  
          final username = data['username'] as String;
          final emailAdd = data['emailAdd'] as String;
          final country = data['country'] as String;
          final bio = data['bio'] as String;
          final profilePicUrl = data['profilePicUrl'] as String;
  
          setState(() {
            _userName = username;
            _emailAddress = emailAdd;
            _country = country;
            _bio = bio;
            _profileUrl = profilePicUrl;
          });
        }
      });
    }
  
    void _retrieveFriends() {
      final friendsDb = FirebaseDatabase.instance
          .ref()
          .child('users')
          .child(uid)
          .child(&quot;friends&quot;);
      _friendsStream = friendsDb.onValue.listen((event) {
        if (event.snapshot.exists) {
          final data = new Map&lt;dynamic, dynamic&gt;.from(
              event.snapshot.value as Map&lt;dynamic, dynamic&gt;);
  
          theirFriends.clear();
          data.forEach((key, value) {
            friendsDb.child(key).onValue.listen((event) {
              final acc = new Map&lt;dynamic, dynamic&gt;.from(
                  event.snapshot.value as Map&lt;dynamic, dynamic&gt;);
              final userID = acc['userID'] as String;
  
              theirFriends.add(userID);
            });
          });
        }
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return userCardContent();
    }
  
    Container userCardContent() {
      return new Container(
        child: ClipOval(
          child: SizedBox(
              width: 110,
              height: 110,
              child: (_profileUrl != null)
                  ? Image.network(_profileUrl, fit: BoxFit.cover)
                  : Image.asset(
                'assets/Blank-Avatar.png',
                fit: BoxFit.cover,
              )),
        ),
      );
    }
  
    @override
    void deactivate() {
      _userStream.cancel();
      _friendsStream.cancel();
      super.deactivate();
    }
  }
  </code></pre>