flutter sqflite 如何接收一个 'todo' 对象来显示值?
flutter sqflite how to receive a 'todo' object to display the values?
我正在使用 sqflite 从 OnCreate 进行插入。这些插入工作正常,我可以在 ListView 中查看项目:
但是当我触摸该列表中的项目时,出现此错误:
这是我的列表代码,可以正常工作,我将对象 'todo' 发送到另一个屏幕的地方:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
//...
),
这里我连接bd、快照和发送对象'todo':
body: FutureBuilder<List<todo>>(
future: _todo,
builder: (BuildContext context, AsyncSnapshot<List<todo>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: const CircularProgressIndicator(),
);
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
var items = snapshot.data ?? <todo>[];
return Container(
margin: EdgeInsets.symmetric(
horizontal: 5.0,
vertical: 5.0,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
),
child: ListView.builder(
itemCount: items.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
//HERE I SEND THE OBJECT 'todo' IN INDEX SELECTEDTO DETAILSCREEN
builder: (context) => DetailScreen(td: items[index]),
),
);
},
child: Card(
child: ListTile(
leading: SizedBox(
height: 100.0,
width: 80.0,
child: Image.network(items[index].imgcourse),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
title: Text(
items[index].title,
style: TextStyle(
fontSize: 20,
color: Colors.blueAccent,
),
),
)),
);
},
),
);
}
},
),
);
}
这是我收到 'todo' 对象的 DetailScreen
:
class DetailScreen extends StatelessWidget {
todo td;
DetailScreen({required this.td});
late DatabaseHandler handler;
late Future<List<todo>> _todo;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[850],
appBar: AppBar(
title: Text(
td.title,
style: TextStyle(
fontSize: 16.0, /*fontWeight: FontWeight.bold*/
),
),
centerTitle: true,
),
body: Container(
padding: EdgeInsets.symmetric(
horizontal: 8,
),
color: Colors.grey[800],
child: SingleChildScrollView(
child: Column(
children: [
//NAME ITEM
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(vertical: 3, horizontal: 5),
child: RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 15.0,
color: Colors.white,
),
children: <TextSpan>[
new TextSpan(
text: 'Nombre: ',
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blueAccent)),
new TextSpan(text: td.title),
],
),
),
),
SizedBox(
height: 10,
),
// DESCRIPTION TIEM
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(horizontal: 5.0),
child: RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 15.0,
color: Colors.white,
),
children: <TextSpan>[
new TextSpan(
text: 'Detalles: ',
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blueAccent)),
new TextSpan(text: td.description),
],
),
),
),
],
),
),
);
),
);
}
}
这是我的'todo'class:
我想知道如何接收对象并读取其中的项目,我是 sqflite 和 flutter 中的新手。
你不需要再次调用“DetailsScream”中的FutureBuilder,你已经传递了参数tb
class DetailScreen extends StatelessWidget {
todo td;
DetailScreen({required this.td});
late DatabaseHandler handler;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[850],
appBar: AppBar(
title: Text(
td.title,
style: TextStyle(
fontSize: 16.0, /*fontWeight: FontWeight.bold*/
),
),
centerTitle: true,
),
body: Container(
padding: EdgeInsets.symmetric(
horizontal: 8,
),
color: Colors.grey[800],
child: SingleChildScrollView(
child: Column(
children: [
//NAME ITEM
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(vertical: 3, horizontal: 5),
child: RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 15.0,
color: Colors.white,
),
children: <TextSpan>[
new TextSpan(
text: 'Nombre: ',
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blueAccent)),
new TextSpan(text: td.title),
],
),
),
),
SizedBox(
height: 10,
),
// DESCRIPTION TIEM
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(horizontal: 5.0),
child: RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 15.0,
color: Colors.white,
),
children: <TextSpan>[
new TextSpan(
text: 'Detalles: ',
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blueAccent)),
new TextSpan(text: td.description),
],
),
),
),
],
),
),
),
);
}
}
告诉你这是否对你有用
我正在使用 sqflite 从 OnCreate 进行插入。这些插入工作正常,我可以在 ListView 中查看项目:
但是当我触摸该列表中的项目时,出现此错误:
这是我的列表代码,可以正常工作,我将对象 'todo' 发送到另一个屏幕的地方:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
//...
),
这里我连接bd、快照和发送对象'todo':
body: FutureBuilder<List<todo>>(
future: _todo,
builder: (BuildContext context, AsyncSnapshot<List<todo>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: const CircularProgressIndicator(),
);
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
var items = snapshot.data ?? <todo>[];
return Container(
margin: EdgeInsets.symmetric(
horizontal: 5.0,
vertical: 5.0,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
),
child: ListView.builder(
itemCount: items.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
//HERE I SEND THE OBJECT 'todo' IN INDEX SELECTEDTO DETAILSCREEN
builder: (context) => DetailScreen(td: items[index]),
),
);
},
child: Card(
child: ListTile(
leading: SizedBox(
height: 100.0,
width: 80.0,
child: Image.network(items[index].imgcourse),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
title: Text(
items[index].title,
style: TextStyle(
fontSize: 20,
color: Colors.blueAccent,
),
),
)),
);
},
),
);
}
},
),
);
}
这是我收到 'todo' 对象的 DetailScreen
:
class DetailScreen extends StatelessWidget {
todo td;
DetailScreen({required this.td});
late DatabaseHandler handler;
late Future<List<todo>> _todo;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[850],
appBar: AppBar(
title: Text(
td.title,
style: TextStyle(
fontSize: 16.0, /*fontWeight: FontWeight.bold*/
),
),
centerTitle: true,
),
body: Container(
padding: EdgeInsets.symmetric(
horizontal: 8,
),
color: Colors.grey[800],
child: SingleChildScrollView(
child: Column(
children: [
//NAME ITEM
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(vertical: 3, horizontal: 5),
child: RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 15.0,
color: Colors.white,
),
children: <TextSpan>[
new TextSpan(
text: 'Nombre: ',
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blueAccent)),
new TextSpan(text: td.title),
],
),
),
),
SizedBox(
height: 10,
),
// DESCRIPTION TIEM
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(horizontal: 5.0),
child: RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 15.0,
color: Colors.white,
),
children: <TextSpan>[
new TextSpan(
text: 'Detalles: ',
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blueAccent)),
new TextSpan(text: td.description),
],
),
),
),
],
),
),
);
),
);
}
}
这是我的'todo'class:
我想知道如何接收对象并读取其中的项目,我是 sqflite 和 flutter 中的新手。
你不需要再次调用“DetailsScream”中的FutureBuilder,你已经传递了参数tb
class DetailScreen extends StatelessWidget {
todo td;
DetailScreen({required this.td});
late DatabaseHandler handler;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[850],
appBar: AppBar(
title: Text(
td.title,
style: TextStyle(
fontSize: 16.0, /*fontWeight: FontWeight.bold*/
),
),
centerTitle: true,
),
body: Container(
padding: EdgeInsets.symmetric(
horizontal: 8,
),
color: Colors.grey[800],
child: SingleChildScrollView(
child: Column(
children: [
//NAME ITEM
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(vertical: 3, horizontal: 5),
child: RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 15.0,
color: Colors.white,
),
children: <TextSpan>[
new TextSpan(
text: 'Nombre: ',
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blueAccent)),
new TextSpan(text: td.title),
],
),
),
),
SizedBox(
height: 10,
),
// DESCRIPTION TIEM
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(horizontal: 5.0),
child: RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 15.0,
color: Colors.white,
),
children: <TextSpan>[
new TextSpan(
text: 'Detalles: ',
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blueAccent)),
new TextSpan(text: td.description),
],
),
),
),
],
),
),
),
);
}
}
告诉你这是否对你有用