jeudi 29 avril 2021

How to Make Checkbox Flutter Dynamically From Mysql?

I want to make dynamically checkbox from data in database mysql. But i don't know how to make it. the error that appears when executing the code is "This function has a return type of 'Widget', but doesn't end with a return statement. Try adding a return statement, or changing the return type to 'void'." And "This function has a return type of 'FutureOr', but doesn't end with a return statement. Try adding a return statement, or changing the return type to 'void'."

class TambahDataTanaman extends StatefulWidget {
  @override
  _TambahDataTanamanState createState() => _TambahDataTanamanState();
}

class _TambahDataTanamanState extends State<TambahDataTanaman> {
  bool selected = false;
  var dataTanah = List<bool>();
  Future<bool> getData() async {
    final response = await http.get("http://192.168.1.12/tanampedia/tanahtampil.php");
    var jsonData = json.decode(response.body);

    List dataTanah2 = [];

    for (var u in jsonData) {
      var data = jsonData(u["id"], u["nama"]);

      dataTanah2.add(data);
      dataTanah.add(false);
    }
    print(dataTanah2.length);
  }

  Widget build(BuildContext context) {
return Scaffold(
        appBar: AppBar(
          title: Text("Tambah Data Tanaman"),
          backgroundColor: Colors.green,
        ),
        body: SingleChildScrollView(
          child: Form(
            key: addTanamanKey,
            child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: ListView(
                children: [
                  new Column(
                    children: [
                     
                      Padding(
                        padding: const EdgeInsets.all(10.0),
                        child: _image == null
                            ? new Text(
                                "Tidak Ada Gambar yang Dipilih, Silahkan Pilih Gambar")
                            : new Image.file(_image),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          new RaisedButton(
                            child: Icon(Icons.image),
                            onPressed: getImageGallery,
                          ),
                        ],
                      ),
                      FutureBuilder<bool>(
                        future: getData(),
                        builder: (context, snapshot){
                        if(snapshot.hasData){
                          if(snapshot.data != null){
                            return CheckboxListTile(title: const Text('Tidak Ada'),
                            value: snapshot.data,
                            onChanged: (val){
                              setState(() {
                                                              
                                                            });
                            });
                          }
                        }
                      },),
                      new RaisedButton(
                          child: new Text(
                            "Tambah Data",
                            style: TextStyle(color: Colors.white),
                          ),
                          color: Colors.green,
                          onPressed: () {}),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ));}
}

there are my code for checkbox, but i am confused how to solve the problem about checkbox dynamically retrieve data from database mysql.




Aucun commentaire:

Enregistrer un commentaire