value displayed after hot reload but null after click
553
Solution 1
call SetState() inside your method, and maybe you will get a null error inside build, so its better add one CircularProgressIndicator,
bool isLoading=false;
@override
void initState() {
super.initState();
loadData();
}
void loadData() async {
final response = await http.get(remoteURL + urldest + '?nomtri=${widget.nomtri}');
if (response.statusCode == 200) {
setState(() {
tgl = isi[0]['tgl'];
dokter = isi[0]['dokter'];
paket = isi[0]['paket'];
keterangan = isi[0]['keterangan'];
isLoading=true;
});
} else {
throw Exception('Gagal Ambil Data Perawatan');
}
}
Widget build(BuildContext context){
if(!isLoading)
{
return Center(child:CircularProgressIndicator());
}
else {retur Container();}
Solution 2
You need to call setState()
to cause Flutter to re-render when state changes.
void loadData() async {
final response = await http.get(remoteURL + urldest + '?nomtri=${widget.nomtri}');
if (response.statusCode == 200) {
List<dynamic> isi = jsonDecode(response.body);
setState(() {
tgl = isi[0]['tgl'];
dokter = isi[0]['dokter'];
paket = isi[0]['paket'];
keterangan = isi[0]['keterangan'];
});
} else {
throw Exception('Gagal Ambil Data Perawatan');
}
}
Author by
M. Syamsul Arifin
Updated on December 08, 2022Comments
-
M. Syamsul Arifin over 1 year
I want to retrieve data from json, after click (form another file), I get null for all values ($tgl, $dokter, $paket, etc), but when I hot-reload (at this below codes) the results are there and are corrects.
what's wrong with my codes ?
class DetilTreatment extends StatefulWidget{ final String nomtri; final String jenis; DetilTreatment(this.nomtri,this.jenis); @override State<StatefulWidget> createState() { return DetilTreatmentState(); } } class DetilTreatmentState extends State<DetilTreatment>{ String urldest='json_detail_rawat_dart.php'; String tgl,dokter,paket,keterangan; List tindakan,terapis; @override void initState() { super.initState(); loadData(); } void loadData() async { final response = await http.get(remoteURL + urldest + '?nomtri=${widget.nomtri}'); if (response.statusCode == 200) { List<dynamic> isi = jsonDecode(response.body); tgl = isi[0]['tgl']; dokter = isi[0]['dokter']; paket = isi[0]['paket']; keterangan = isi[0]['keterangan']; } else { throw Exception('Gagal Ambil Data Perawatan'); } } @override Widget build(BuildContext context) { return new Scaffold( backgroundColor: Colors.blue[50], appBar: new AppBar( title: const Text('Data Riwayat Perawatan'), ), body: ListView( padding: EdgeInsets.all(15.0), children: <Widget>[ // all results are null, but when hot-reload the results displayed ! Text('Tgl/Jam : $tgl', textAlign: TextAlign.left), Text('Nama Dokter : $dokter', textAlign: TextAlign.left), Text('Paket : $paket', textAlign: TextAlign.left), Text('Treatment : ', textAlign: TextAlign.left), Text('Keterangan : $keterangan', textAlign: TextAlign.left), ], ) ); } }
-
M. Syamsul Arifin over 5 yearsyes, it's work, but I must clicked 2x from source link
-
M. Syamsul Arifin over 5 yearsthank you very much, it's work well ! but sorry I cannot click this as usefull answer, because my reputation is low :D
-
Günter Zöchbauer over 5 years"I must clicked 2x from source link" sorry, but I don't understand what you mean? What is "source link"?
-
M. Syamsul Arifin over 5 yearssorry with my english, I mean that widget displayed after I clicked from another file. this is code : onTap: () { Navigator.of(context).push(new MaterialPageRoute<Null>( builder: (BuildContext context) { return new DetilTreatment(treatment[index]['nomtri'],treatment[index]['jenis']); }, fullscreenDialog: true) ); }, so, first click onTap -> null, second click -> correct value. thank you for your help
-
Günter Zöchbauer over 5 yearsI don't see why you would need to click twice, except if you run into the error satish soni mentioned in the other answer.
-
M. Syamsul Arifin over 5 yearsI only add CircularProgressIndicator() at build, like @satish soni answer below, and it's work
-
Günter Zöchbauer over 5 yearsOk, so if that fixes your problem then you should accept his answer.