1

我想从 json 中检索数据,单击(形成另一个文件)后,所有值($tgl、$dokter、$paket 等)都为空,但是当我热重载时(在下面的代码中)结果是有并且有更正。

我的代码有什么问题?

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),
          ],
        )
        );
    }
}
4

2 回答 2

1

您需要调用setState()以使 Flutter 在状态更改时重新渲染。

  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');
    }
  }
于 2019-01-21T08:21:46.633 回答
0

在你的方法中调用 SetState(),也许你会在构建中得到一个空错误,所以最好添加一个 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();}
于 2019-01-21T09:14:41.237 回答