You have lost yourself amid the asynchronous methods ♪ and then()...There are two ways to do what you want, but I will focus on the question of FutureBuilder.First we will modify your method Data() as follows:List<Produtos> dados() async {
List<Produtos> produtos = [];
produtos = await db.getAllProdutos();
if(produtos.isEmpty()){
await endpoints.resgata_produtos(sUser, sCnpj, sChave);
context.read<DadosAcesso>().listProd.clear();
produtos = await dados();
} else {
produtos.forEach((element) {
context.read<DadosAcesso>().listProd.add(Produto(
codbarras: element['codbarras'],
desproduto: element["desproduto"]));
});
}
return produtos;
}
Thus, using only the ♪, your method will wait every call to then return to your FutureBuilder all available data.Now it is necessary to adjust your FutureBuilder, we will do as follows:FutureBuilder(
future: dados(),
builder: (BuildContext context, AsyncSnapshot<List<Map<String, dynamic>>> snapshot) {
if (snapshot.hasData) {
DTS dts = DTS(snapshot.data);
return Container(
child: ListView(
children: [
PaginatedDataTable(
sortColumnIndex: 1,
columnSpacing: 3,
dataRowHeight: 55,
horizontalMargin: 3,
headingRowHeight: 30,
header: Text('Lista de Produtos'),
columns: [
DataColumn(
label: Text(
'Cód.',
style: TextStyle(
fontStyle: FontStyle.normal,
fontWeight: FontWeight.bold,
fontSize: 15),
),
),
DataColumn(
label: Text(
'Descrição',
style: TextStyle(
fontStyle: FontStyle.normal,
fontWeight: FontWeight.bold,
fontSize: 18),
),
),
DataColumn(
label: Text(
'QTDE',
style: TextStyle(
fontStyle: FontStyle.normal,
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
],
source: dts,
onRowsPerPageChanged: (r) {
setState(() {
_rowPerPage = r;
});
},
rowsPerPage: _rowPerPage,
),
],
),
);
} else {
return circularProgressGP();
}
},
);
ExplanationO FutureBuilder will call the method Data(), which in turn will fetch in the database all the necessary information, if you do not find will try to request for the API.
Made this, you will return the data available for the FutureBuilder that will provide the values within snapshot.data that in your case is a list of Maps.In this way, FutureBuilder after treated all requests, you can make the call of your class DTSIf there is information available.Note: Since you didn't tell us the product list type, I played something
generic List<Produtos>, there you make the necessary changes. .