I have this code that generates a listview.builder with the items extracted from the database. Now, I need that when I touch each item, the id of that item is passed to the next screen to work with it and do another procedure.
The function of moving to the other page works correctly, but when including the vehicle.id within the procedure, the function throws an error. The database is SQFlite.
The idea is to pass that parameter to several screens to generate specific calculations with each one of the id's separately.
import 'package:flutter/material.dart';
import 'package:mipicoyplaca/modelos/vehiculo_model.dart';
import 'package:mipicoyplaca/paginas/mipicoyplaca.dart';
import 'package:mipicoyplaca/providers/db_provider.dart';
class VehiculosPage extends StatefulWidget {
@override
_VehiculosPageState createState() => _VehiculosPageState();
}
class _VehiculosPageState extends State<VehiculosPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.symmetric(vertical: 30.0, horizontal: 10.0),
child: _crearListaVehiculos()),
);
}
Widget _crearListaVehiculos() {
return FutureBuilder(
future: DBProvider.db.getTodosVehiculos(),
builder:
(BuildContext context, AsyncSnapshot<List<VehiculoModel>> snapshot) {
if (snapshot.hasData) {
final vehiculos = snapshot.data;
return ListView.builder(
itemCount: vehiculos.length,
itemBuilder: (context, i) => _crearItem(context, vehiculos[i]),
);
} else {
return Center(child: CircularProgressIndicator());
}
},
);
}
Widget _crearItem(BuildContext context, VehiculoModel vehiculo) {
return Dismissible(
key: UniqueKey(),
onDismissed: (direccion) {
DBProvider.db.deleteVehiculo(vehiculo.id);
},
child: Column(
//crossAxisAlignment: CrossAxisAlignment.center,
//mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
RaisedButton(
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.black),
borderRadius: new BorderRadius.circular(10.0)),
elevation: 15.0,
color: Colors.yellow,
child: Text('${vehiculo.placa}',
style: TextStyle(
color: Colors.black,
fontSize: 80.0,
fontWeight: FontWeight.bold)),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MiPicoyPlaca(vehiculo.id)), //AQUÍ ESTA EL ERROR AL PASAR EL ARGUMENTO
);
}),
Text('Desliza para eliminiar vehiculo',
style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold)),
SizedBox(
height: 20.0,
)
],
),
);
}
}
I answer to myself, all you have to do is send a named argument to the next screen. And on that screen capture it and work on it.