当我通过页面传递id(整数数据)时,我收到一条错误消息,提示genre_id为null。因此,当我使用索引作为变量将id传递到下一页时,出现了问题。如何使用索引将其作为下一页的ID传递?
在我使用的sqlite数据库中,我有一个tbl_genres和一个tbl_books,这些书的条目都通过genre_id(两个表中的一列)绑定到类型表。
@override
Widget build(BuildContext context) {
return Scaffold(
body: !loading ? new ListView.builder(
itemCount: genreList.length,
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new ListTile(
title: new Text("${genreList[index]}"),
onTap: () {
Navigator.push(context,
MaterialPageRoute(
builder: (context) =>
BookListPage(id: index), //how to pass index as an int?
),
);
}),
);
},
) : CircularProgressIndicator(),
);
}
这是我的下一页...
class BookListPage extends StatefulWidget {
int id;
BookListPage({this.id});
@override
_BookListPageState createState() => _BookListPageState();
}
class _BookListPageState extends State<BookListPage> {
bool loading;
List<Map> bookNames;
final int id;
_BookListPageState({this.id});
void initState() {
super.initState();
loading = true;
getBookData();
}
Future getBookData() async {
print(id);
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "asset_sample_sqlite.db");
ByteData data = await rootBundle.load(join("assets", "sample_sqlite.db"));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await new File(path).writeAsBytes(bytes);
Database db = await openDatabase(path);
final _bookList = await db.rawQuery('SELECT book_name[] FROM tbl_books WHERE genre_id = $id'); //how to reference the passed id?
await db.close();
setState((){
loading = false;
bookNames = _bookList;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: !loading ? new ListView.builder(
itemCount: bookNames.length,
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new ListTile(
title: new Text("${bookNames[index]}"),
),
);
}
) : CircularProgressIndicator(),
);
}
}
还有如何在rawQuery中使用该索引来显示仅与该ID有关的信息?
答案 0 :(得分:0)
如下修改您的第二页:
window.history.go(-1-n)