I am new to flutter. Following is my code for getting data from a firestore collection called posts.The problem when the first times app loaded it takes few seconds to load data until that it show a blank screen.It only happens only the first time.So how to display loading spinner or something to user until data gets loaded? (Not that this is not the whole code)
class ForumHome extends StatefulWidget {
@override
_ForumHomeState createState() => _ForumHomeState();
}
dbMethods dbcrud = new dbMethods();
class _ForumHomeState extends State<ForumHome> {
String title, content;
//Subscribing for post details
StreamSubscription<QuerySnapshot> subscription;
List<DocumentSnapshot> snapshot;
CollectionReference collectionReference =
Firestore.instance.collection("posts");
@override
void initState() {
super.initState();
subscription = collectionReference.snapshots().listen((datasnapshot) {
setState(() {
snapshot = datasnapshot.documents;
});
});
}
@override
Widget build(BuildContext context) {
int num = snapshot?.length ?? 0;
return Scaffold(
appBar: AppBar(title: Text('Idea Forum '), backgroundColor: Colors.pink),
body: new ListView.builder(
itemCount: num,
itemBuilder: (context, index) {
return new Card(
elevation: 15.0,
margin: EdgeInsets.all(10.0),
child: new ListTile(
onTap: () {
//For udating the view count
snapshot[index]
.reference
.updateData({'Views': snapshot[index].data['Views'] + 1});