DropdownButton<List> using local data - Do not list items on the screen, only after hot reload

224 Views Asked by At

I'm new to the flutter and I don't know how to solve this problem.

I have a List with await method, but my screen does not await for the list to load to list, only when I update with the hot-reload, the screen works.

My async method

ListaRefeitorio? _selecione;
    List<ListaRefeitorio> _refeitorios = <ListaRefeitorio>[];
    RefeitorioController controller = new RefeitorioController();
  
    @override
    void initState() {
      super.initState();
      _listarRefeitorios();
    }

My Screen

@override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBarControleAcessoWidget("Refeitório"),
            body: Column(
              children: [
                SizedBox(height: 30),
                Container(
                  child: Center(
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        padding: EdgeInsets.only(left: 16, right: 16),
                        decoration: BoxDecoration(
                          border:
                              Border.all(color: AppColors.chartSecondary, width: 1),
                          borderRadius: BorderRadius.circular(15),
                        ),
                        child: DropdownButton<ListaRefeitorio>(
                          hint: Text("Selecione Refeitório"),
                          dropdownColor: AppColors.white,
                          icon: Icon(Icons.arrow_drop_down),
                          iconSize: 36,
                          isExpanded: true,
                          underline: SizedBox(),
                          style: TextStyle(
                            color: AppColors.black,
                            fontSize: 20,
                          ),
                          value: _selecione,
                          onChanged: (ListaRefeitorio? novoValor) {
                            setState(() {
                              _selecione = novoValor;
                            });
                          },
                          items: _refeitorios.map((ListaRefeitorio valueItem) {
                            return new DropdownMenuItem<ListaRefeitorio>(
                              value: valueItem,
                              child: new Text(valueItem.acessoPontoAcessoDescricao),
                            );
                          }).toList(),
                        ),
                      ),
                    ),
                  ),
                ),
                Container(),
                Expanded(
                  child: GridView.count(
                    crossAxisSpacing: 12,
                    mainAxisSpacing: 12,
                    crossAxisCount: 2,
                    children: [
                      Container(
                        child: SizedBox.expand(
                          child: FlatButton(
                            child: CardsWidget(
                              label: "Ler QR Code",
                              imagem: AppImages.scanQrCode,
                            ),
                            onPressed: () {
                              scanQRCode();
                            },
                          ),
                        ),
                      ),
                      Container(
                        child: SizedBox.expand(
                          child: FlatButton(
                            child: CardsWidget(
                                label: "Sincronizar Dados", imagem: AppImages.sync),
                            onPressed: () {
                              controller.sincronizar();
                              // RefeitorioService.listarRefeitorio();
                            },
                          ),
                        ),
                      ),
                      SizedBox(height: 30),
                      Text("Resultado"),
                      Text(QRCode),
                      Text(DataHora),
                      Text(_selecione.toString()),
                    ],
                  ),
                ),
              ],
            ));
      }

I've tried using the futurebuilder but I don't think that's my problem.

I don't know what to do anymore

1

There are 1 best solutions below

0
On

I had the same issue with the DropDownButton list only displaying because of the Hot Reload refreshing the state.

When using a custom mapping of a List remember to use setState() in the method that populates the List with data (in my case it was pulling from Sqflite).

//This populate method would be called in either initstate or afterFirstLayout
populateDataList() {
    await controller.getList().then((list) => 
        setState(() {
            _refeitorios = list;
        })
    );
}