0
Good afternoon, I have a problem, I have my code below, and in it I do the following, when it opens the screen for the first time it searches data from an api and saved in the database. Soon the user can close the app and open it again, when he opens the screen and already has the data he searches directly in the database and fills the Paginateddatatable, calms the second option works properly, the database data are fine, the problem is when I run the api, inside the api already has the process of saving in the bank, saved right without problems, but wait there and do not fill my Paginateddatatable, follow the code below all, thanks for the help.
import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:....../conexao/endpoints_api.dart';
import 'package:....../api/produtos/produto.dart';
import 'package:balanco_app/helpers/database_helper.dart';
import 'package:balanco_app/models/dados_acesso.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class ListaDeProdutos extends StatefulWidget {
@override
_ListaDeProdutosState createState() => _ListaDeProdutosState();
}
class _ListaDeProdutosState extends State<ListaDeProdutos> {
DatabaseHelper db = DatabaseHelper();
Endpoints endpoints = Endpoints();
bool sort;
Color primaryColor;
int _rowPerPage = PaginatedDataTable.defaultRowsPerPage;
String sCnpj;
String sUser;
String sChave;
@override
void initState() {
super.initState();
resgataDados();
sCnpj = context.read<DadosAcesso>().cnpjInicial;
sUser = context.read<DadosAcesso>().usuario;
sChave = context.read<DadosAcesso>().codAcesso;
}
@override
Widget build(BuildContext context) {
primaryColor = Theme.of(context).primaryColor;
DTS dts = DTS(context.read<DadosAcesso>().listProd);
setState(() {
dados();
});
return FutureBuilder(
future: db.getAllProdutos(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
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();
}
},
);
}
resgataDados() async {
List linhas = await db.getDsDados();
for (var linha in linhas) {
sCnpj = linha['cnpjInicial'].toString();
sUser = linha['usuario'].toString();
sChave = linha['codAcesso'].toString();
}
}
dados() async {
await db.getAllProdutos().then((value) async{
value.forEach((element) {
context.read<DadosAcesso>().listProd.add(Produto(
codbarras: element['codbarras'],
desproduto: element["desproduto"]));
});
print('Lista length: ${context.read<DadosAcesso>().listProd.length}');
if(context.read<DadosAcesso>().listProd.length == 0){
await endpoints.resgata_produtos(sUser, sCnpj, sChave);
context.read<DadosAcesso>().listProd.clear();
await db.getAllProdutos().then((value) {
value.forEach((element) {
context.read<DadosAcesso>().listProd.add(Produto(
codbarras: element['codbarras'],
desproduto: element["desproduto"]));
});
}).catchError((error) {
print(error);
});
}
}).catchError((error) {
print(error);
});
}
}
circularProgressGP() {
return Container(
color: Colors.white,
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 100,
),
SizedBox(
height: 90,
width: 90,
child: CircularProgressIndicator(backgroundColor: Colors.white),
),
SizedBox(
height: 40,
),
RotateAnimatedTextKit(
duration: const Duration(milliseconds: 2000) ,
onTap: () {},
text: ["Conectando...", "Buscando Dados...", "Aguarde..."],
textStyle: TextStyle(fontSize: 30, color: Colors.black),
textAlign: TextAlign.start
),
],
)
),
);
}
class DTS extends DataTableSource{
DTS(this.listProd);
DatabaseHelper db = DatabaseHelper();
Material material = Material();
final List<Produto> listProd;
@override
DataRow getRow(int index) {
assert(index >= 0);
if (index >= listProd.length) return null;
final row = listProd[index];
print('Index: $index');
return DataRow.byIndex(
index: index,
color: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected))
return material.color.withOpacity(0.08);
if (index % 2 == 0) return Colors.grey.withOpacity(0.3);
return null;
}),
cells: [
DataCell(
Container(
constraints: BoxConstraints(maxWidth: 150),
child: Text(row.codbarras),
),
),
DataCell(
Container(
constraints: BoxConstraints(maxWidth: 150),
child: Text(
row.desproduto,
style: TextStyle(fontSize: 15),
),
),
),
DataCell(
Center(
child: Container(
constraints: BoxConstraints(maxWidth: 110),
child: TextField(
keyboardType: TextInputType.number,
//controller: qtde_controller,
onChanged: (text) {
print("Texto digitado: $text");
},
onTap: (){
},
),
),
),
),
]);
}
@override
bool get isRowCountApproximate => true;
@override
int get rowCount => 10;
@override
int get selectedRowCount => 0;
}
What I’m doing wrong, because whenever the api runs and pulls the data and saved in the database, but does not present to me on screen, the api is right, I believe it should be something from Paginateddatatable, I appreciate the help.
What is within the method
db.getAllProdutos()
? Your screen will only be updated when you return any valid data in the call of that method that the Futurebuilder is listening... the way you did calling the methodsetState(() { dados(); });
will not draw anything on screen, because inside the Futurebuilder you are validating if it has some data and not the properties of the screen itself.– Matheus Ribeiro
The method has the following: Future<List<Map<String, Dynamic>>> getAllProducts() async { Database db = await this.database; print('passing...'); Return await db.rawQuery('SELECT * FROM product'); }
– Fernando Alexandre Wahl
What I could do is put the data() directly into the Futrebuilder, it may be for sure. But then he keeps processing and the information doesn’t come. I’m lost....
– Fernando Alexandre Wahl
Fernando, do not edit the question by adding "[solved]" to the title. [pt.so] is not a discussion forum. You have your own tools to signal that the question has been answered. See What should I do if someone answers my question?. If the current answer does not meet 100%, make your answer explaining the solution.
– Woss