0
import 'package:flutter/material.dart';
import 'package:ouvinos_caprinos/especie/class/especie.dart';
import 'package:ouvinos_caprinos/especie/db/especie_database.dart';
import 'package:ouvinos_caprinos/ui/caprino/caprino_page.dart';
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
EspecieHelper _especieHelper = EspecieHelper();
List<Especie> especies = List();
@override
void initState() {
_getAllEspecies();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Rebanhos Disponiveis"),
backgroundColor: Colors.green,
centerTitle: true,
),
body: ListView.builder(
padding: EdgeInsets.all(10.0),
itemCount: especies.length,
itemBuilder: (context, index) {
return _especieDisponiveis(context, index);
}),
);
}
Widget _especieDisponiveis(BuildContext context, int index) {
return GestureDetector(
child: Card(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
Container(
width: 80.0,
height: 80.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(
"images/" + especies[index].descricao.toLowerCase() + ".png"),
fit: BoxFit.cover),
),
),
Padding(
padding: EdgeInsets.only(left: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
capitalize(especies[index].descricao + "s"),
style: TextStyle(
fontSize: 22.0, fontWeight: FontWeight.bold),
),
],
),
)
],
),
),
),
onTap: () {
_goToPage(index);
},
);
}
void _goToPage(int index) {
if (index == 0) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CaprinoPage(),
),
);
} else {
Navigator.push(
context,
MaterialPageRoute(
// builder: (context) => OvinoPage(),
),
);
}
}
String capitalize(String string) {
if (string == null) {
throw ArgumentError("string: $string");
}
if (string.isEmpty) {
return string;
}
return string[0].toUpperCase() + string.substring(1);
}
Future<void> _getAllEspecies() async {
await _especieHelper.getAllEspecies().then((listaE) {
setState(() {
especies = listaE;
});
});
}
}
well I’m trying to make my app less static, I’m taking the information of species from my database to improve usability and for future implementations, but I’ve been encountering failed attempts because every time I start the app( for the first time) the specific database is created until then OK, but it does not load the information to the listview, I can only view them after closing the app and open again, someone can help me ?
At first it seems that the return of
_getAllEspecies()
only happens after starting status maybe using aFutureBuilder
resolve.– GeekSilva
I tried several ways, I used Futurebuilder to test, but I could not, only appeared after closing the app and opening dnv (in case a way to update the page), I may not have understood the concept of Futurebuilder
– MatheusPatriota
How is the method
_especieHelper.getAllEspecies()
?– GeekSilva
Future<List> getAllEspecies() async {
 Database dbEspecie = await db;
 List listMap = await dbEspecie.rawQuery("SELECT * FROM $tableName");
 List<Especie> listEspecie = List();
 for (Map m in listMap) {
 listEspecie.add(Especie.fromMap(m));
 }
 return listEspecie;
 }
– MatheusPatriota
Right, you can apply in Futurebuilder, I’ll leave in the answer.
– GeekSilva