A few subpages in my app do not display the content from the database right away. They show the content after I save the content (hot reload) though.
There are just a few subpages in my app I notice this behaviour though, and all of them have something in common: The display information from the database.
I implemented a few print statements in my code which print the information I receive from the datatabase though. So, it can not be due to a lack of information/information not received yet.
Do you have an idea on how to fix it?
Code:
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:nutritious/custom_widgets/AppBar_Custom.dart';
import 'package:nutritious/custom_widgets/AppBar_SearchIcon.dart';
import 'package:nutritious/datenbank/firestore.dart';
import 'package:nutritious/custom_widgets/AppBar_ProfileIcon.dart';
class Krankheiten extends StatefulWidget {
@override
_KrankheitenState createState() => _KrankheitenState();
}
class _KrankheitenState extends State<Krankheiten> {
_KrankheitenState();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
/*username.dispose();
password.dispose();
super.dispose();*/
}
List<String> krankheit_name = [];
List<String> krankheit_beschreibung = [];
getKrankheiten() async {
await FirebaseFirestore.instance
.collection("krankheiten")
.orderBy("name")
.get()
.then((snapshot) => {
snapshot.docs.forEach((doc) {
krankheit_name.add(doc.data()["name"]);
krankheit_beschreibung.add(doc.data()["beschreibung"]);
})
});
for (int i = 0; i < krankheit_name.length; i++) {
print("+++");
print(krankheit_name.length);
print(krankheit_name[i]);
print(krankheit_beschreibung[i]);
}
}
Container createKrankheiten(int i) {
return Container(
width: 500,
color: Colors.white,
margin: EdgeInsets.only(bottom: 15),
padding: EdgeInsets.fromLTRB(15, 15, 15, 15),
child: Padding(
padding: const EdgeInsets.only(top: 15, bottom: 15),
child: Text(krankheit_name[i],
textAlign: TextAlign.left, style: TextStyle(fontSize: 20.0)),
),
);
}
@override
Widget build(BuildContext context) {
getKrankheiten();
return Scaffold(
appBar: AppBarCustom(widgetTitle: "Krankheiten"),
body: SingleChildScrollView(
child: Container(
color: Color.fromRGBO(218, 243, 186, 1),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: [
for (int i = 0; i < krankheit_name.length; i++)
createKrankheiten(i)
],
),
))));
}
}
2
Answers
The
getKrankheiten
function is called inbuild
method but it doesn’t load data synchronously. So data ends up being fetched AFTER the screen is built and the screen doesn’t update post fresh data fetch.You should use a
FutureBuilder
for this:https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
Two mistakes.
awaiting
for the future functiongetKrankheiten()
.setState
when adding to list in the following lines:Two suggestions.
build()
.You can consider calling it in theinitState()
function.futureBuilder
to attain your desired function.