I am new to flutter and have come across a question in flutter.
1 . I already have a Database created in SQLITE.
2 . the database is called "nutrition_program.db". It contains 2 tables called "food" and "food group".
3 . I only need to show the data, I don't need to create, delete or update.
- Only managed to copy the database from assets.
the examples i have seen all create the database. Is there a way to just read them and display the data?
I managed to do something but it only shows me everything in null, but it tells me the total data in the table
this is MAIN.DART
import 'package:flutter/material.dart';
import 'package:pruebabd/dbhelper.dart';
void main() => runApp(AppPrueba());
class AppPrueba extends StatefulWidget {
@override
_AppPruebaState createState() => _AppPruebaState();
}
class _AppPruebaState extends State<AppPrueba> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar:
AppBar(title: Text('Prueba SQLITE'),),
body: FutureBuilder(
future: grupo_alimentos(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else {
return Container(
child: ListView.builder(
itemCount: snapshot.data.length,
scrollDirection: Axis.vertical,
itemBuilder: (BuildContext context, int index){
return Card(
child: positionedBlock(context, snapshot.data[index]));
},
),
);
}
} //cierrre Snapshot
),
),
);
}
}
Widget positionedBlock(BuildContext context, GrupoAlimentos grupoAlimentos) {
return Align(
alignment: Alignment.topCenter,
child: Padding(
padding: const EdgeInsets.only(right: 8.0, top: 8.0),
child: ListTile(
title:Text('${grupoAlimentos.ID}',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
subtitle: Text(
'${grupoAlimentos.Nombre_Grupo}',
style: TextStyle(
fontSize: 18,
),
),
),
),
);
}
and the dbhelper.dart file
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class DbHelper{
Future initDB() async{
final dbpath=await getDatabasesPath();
final path = join (dbpath, "programa_nutricion.db");//nombre de la base de datos a migrar
final exist = await databaseExists(path);
if (exist){
print('La base de Datos existe');
}else{
print('Creando una copia de la base de datos desde Assets');
try {
await Directory(dirname(path)).create(recursive: true);
} catch(_) {}
ByteData data = await rootBundle.load(join("assets","programa_nutricion.db"));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(path).writeAsBytes(bytes, flush: true);
print('BASE DE DATOS COPIADA');
}
await openDatabase(path);
}
}
class GrupoAlimentos{
final int ID;
final String Nombre_Grupo;
GrupoAlimentos({this.ID,this.Nombre_Grupo});
}
final Future<Database> database = getDatabasesPath().then((String path) {
return openDatabase(join(path, 'programa_nutricion.db'));
});
Future<List<GrupoAlimentos>> grupo_alimentos() async {
// Obtiene una referencia de la base de datos
final Database db = await database;
// Consulta la tabla por todos los grupos.
final List<Map<String, dynamic>> maps = await db.query('grupo_alimentos');
// Convierte List<Map<String, dynamic> en List<GrupoAlimentos>.
return List.generate(maps.length, (i) {
return GrupoAlimentos(
ID: maps[i]['id'],
Nombre_Grupo: maps[i]['nombre'],
);
});
}
When executing the APP it gives me this result showing me the 24 records that I have in said database. But with NULL
I attached the image
https://i.stack.imgur.com/VKWbJ.png