Flutter Update Text Widget Variable without Reloading

Issue

Im using Flutter with Firestore to make Investing Application.
enter image description here
In this page, I get Firestore’s data in Streambuilder.
The data what i recieved are displayed in Listview with Inkwell.
What I want to do is when I click the Inkwell, then change the bottom left’s Text Widget’s text into Listview’s number.
So, I used the setstate method with FutureBuilder.
It works but there are some problems.
When I click the Inkwell, then whole page reloads.
So, My Streambuilder widgets displays circleprogressindicator short.
However I think this is not good for users.
I want to solve this problem.
Is there any solution?
My code is in here.
I’m sorry for my poor English and I hope my problem was properly communicated.
https://github.com/RGLie/Investing-Game-ICISTS/blob/main/lib/startup_1_trade.dart

Solution

You can fix the rebuilds by storing the streams in variables outside of the build method and using them in the StreamBuilder widgets.

class Startup1Trade extends StatefulWidget {
  ...
}

class _Startup1TradeState extends State<Startup1Trade> {
  ...

  CollectionReference prices = FirebaseFirestore.instance.collection('startup_1');
  CollectionReference users = FirebaseFirestore.instance.collection('users');

  final Stream priceStream = prices.doc('price').snapshots();
  final Stream userStream = users.doc(widget.user.uid).snapshots();
  
  @override
  Widget build(BuildContext context) {
    ...
  }
  

}

Use them like this:

StreamBuilder<DocumentSnapshot>(
  stream: priceStream,
  builder: (context, snap) {
    ...
  }
}

StreamBuilder<DocumentSnapshot>(
  stream: userStream,
  builder: (context, snap) {
    ...
  }
}

Answered By – Victor Eronmosele

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *