Update data on Setstate() Flutter page, enter Loop

Asked

Viewed 1,630 times

-2

I’m having trouble updating the status I’m taking a list and showing her If I put Setstate in _getProdPro loops, if I don’t put, the data is not updated on the screen, someone knows how I can call correctly

import 'package:gestoque/ConsultaPromocao.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:gestoque/Api.dart';

class ViewProdutosPromocionais extends StatefulWidget {
  @override
  _ViewProdutosPromocionaisState createState() =>
      _ViewProdutosPromocionaisState();
}



class _ViewProdutosPromocionaisState extends State<ViewProdutosPromocionais> {

  var promocoes = new List<ConsultaPromocao>();
  String _search;

  Future<List> _getPromocoes() async {

    Api.getProdPromo().then((response) {

      List lista = json.decode(response.body);
      promocoes =
          lista.map((model) => ConsultaPromocao.fromJson(model)).toList();
    });
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Image.asset("assets/logo_topo_1.png"),
          backgroundColor: Colors.orange,
        ),
        backgroundColor: Colors.white,
        body: Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(10.0),
              child: TextField(
                decoration: InputDecoration(
                    labelText: "Nome do Produto",
                    labelStyle: TextStyle(color: Colors.black),
                    border: OutlineInputBorder()),
                style: TextStyle(color: Colors.black, fontSize: 18.0),
                textAlign: TextAlign.center,
              ),
            ),
            Expanded(
              child: FutureBuilder(
                future: _getPromocoes(),
                builder: (context, snapshot) {
                  switch (snapshot.connectionState) {
                    case ConnectionState.waiting:
                    case ConnectionState.none:
                      return Container(
                        width: 200.0,
                        height: 200.0,
                        alignment: Alignment.center,
                        child: CircularProgressIndicator(
                          valueColor:
                              AlwaysStoppedAnimation<Color>(Colors.orange),
                          strokeWidth: 5,
                        ),
                      );

                    default:
                      if (snapshot.hasError)
                        return Container();
                      else
                        return ListaProdutosPromocionais();
                  }
                },
              ),
            )
          ],
        ));
  }



  ListaProdutosPromocionais() {
    return ListView.builder(
        itemCount: promocoes.length,
        itemBuilder: (context, index) {
          return Container(
              child: Card(
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Text('Código: : ',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.bold)),
                    Text(promocoes[index].codprod),
                  ],
                ),
                Row(
                  children: <Widget>[
                    Text('Nome: ',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.bold)),
                    Text(promocoes[index].descrprod),
                  ],
                ),
                Row(
                  children: <Widget>[
                    Text('Valor R\$ ',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.bold)),
                    Text(promocoes[index].vlrvenda),
                  ],
                ),
              ],
            ),
          ));
        });
  }
}


3 answers

1


Put on a timer to do frequently, to start put on iniState

void initState() {
    super.initState();
     Timer(Duration(seconds: 60), () => setState((){});
}

1

I managed to solve, as follows...

inserted a var for return

    var data =  await Api.getProdPromo().then((response) {
      List lista = json.decode(response.body);
      promocoes =
          lista.map((model) => ConsultaPromocao.fromJson(model)).toList();
    });
  }

then I called the function in the builder..

_ViewProdutosPromocionaisState(){
    _getPromocoes();
  }````

1

How are you making use of a FutureBuilder you don’t need setState or workarounds, just modify your method as follows:

  Future<List> _getPromocoes() async {

    var response await Api.getProdPromo();

    List lista = json.decode(response.body);
    promocoes = lista.map((model) => ConsultaPromocao.fromJson(model)).toList();

    return promocoes;
  }

You’re having trouble with functions async (Asynchronous), I advise to give a better study on...

The way you’re doing it, the use of FutureBuilder.

  • got it.... Vlw will read more on the subject , vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.