我有这段代码,它使用从数据库中提取的项目生成一个 listview.builder。现在,我需要当我触摸每个项目时,将该项目的 id 传递到下一个屏幕以使用它并执行另一个过程。
移动到另一个页面的功能可以正常工作,但是当在过程中包含vehicle.id 时,该功能会引发错误。数据库是 SQFlite。
这个想法是将该参数传递给几个屏幕,以分别生成每个 id 的特定计算。
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,
)
],
),
);
}
}
我对自己说,您所要做的就是将命名参数发送到下一个屏幕。并在该屏幕上捕获并处理它。