Flutter 从现有数据库 sqflite 中获取数据
Flutter fetch Data from existing database sqflite
你好朋友我是初学者学习 sqflite 我的任务是从现有的数据库中获取数据并显示在 listview.builder 我搜索 youtube 和其他资源但没有找到请帮助我这里是我现有的数据库助手 class 和 listview class 我需要显示我的数据库的地方请帮我检查下面的代码
//existing database helper class
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class Dbhelper{
Database? database;
Future<Database?> get db async{
if(database!=null){
print('Database is already existing');
return database;
}
else{
database=await initdb();
return database;
}
}
Future initdb()async {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, "bible.db");
// Check if the database exists
var exists = await databaseExists(path);
if (!exists) {
// Should happen only the first time you launch your application
print("Creating new copy from asset");
// Make sure the parent directory exists
try {
await Directory(dirname(path)).create(recursive: true);
} catch (_) {}
// Copy from asset
ByteData data = await rootBundle.load(join("asset", "bible.db"));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Write and flush the bytes written
await File(path).writeAsBytes(bytes, flush: true);
}
else {
print("Opening existing database");
}
// open the database
return await openDatabase(path, readOnly: true);
}
}
//here i want show my data in list view.builder
import 'dart:math';
import 'package:bibleapp/Verse.dart';
import 'package:flutter/material.dart';
class chapters extends StatefulWidget {
const chapters({Key? key}) : super(key: key);
@override
_chaptersState createState() => _chaptersState();
}
class _chaptersState extends State<chapters> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('SELECT CHAPTER'),
),
body: Padding(
padding: EdgeInsets.all(10),
child: GridView.builder(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 5,
crossAxisSpacing:16,
mainAxisSpacing: 16
),
itemCount: 50,
itemBuilder: (context, index) {
return GestureDetector(
onTap: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Verse()),
);
},
child: CircleAvatar(
backgroundColor:Colors.primaries[Random().nextInt(Colors.primaries.length)],
child: Text(index.toString(),style: TextStyle(
fontSize: 20,
color: Colors.white
),),
),
);
},
),
),
);
}
}
答案在这里
static Future<List<Map<String, dynamic>>> gedata() async {
final db = await Dbhelper().db;
return db!.rawQuery('SELECT * from your table name);
}
你好朋友我是初学者学习 sqflite 我的任务是从现有的数据库中获取数据并显示在 listview.builder 我搜索 youtube 和其他资源但没有找到请帮助我这里是我现有的数据库助手 class 和 listview class 我需要显示我的数据库的地方请帮我检查下面的代码
//existing database helper class
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class Dbhelper{
Database? database;
Future<Database?> get db async{
if(database!=null){
print('Database is already existing');
return database;
}
else{
database=await initdb();
return database;
}
}
Future initdb()async {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, "bible.db");
// Check if the database exists
var exists = await databaseExists(path);
if (!exists) {
// Should happen only the first time you launch your application
print("Creating new copy from asset");
// Make sure the parent directory exists
try {
await Directory(dirname(path)).create(recursive: true);
} catch (_) {}
// Copy from asset
ByteData data = await rootBundle.load(join("asset", "bible.db"));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Write and flush the bytes written
await File(path).writeAsBytes(bytes, flush: true);
}
else {
print("Opening existing database");
}
// open the database
return await openDatabase(path, readOnly: true);
}
}
//here i want show my data in list view.builder
import 'dart:math';
import 'package:bibleapp/Verse.dart';
import 'package:flutter/material.dart';
class chapters extends StatefulWidget {
const chapters({Key? key}) : super(key: key);
@override
_chaptersState createState() => _chaptersState();
}
class _chaptersState extends State<chapters> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('SELECT CHAPTER'),
),
body: Padding(
padding: EdgeInsets.all(10),
child: GridView.builder(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 5,
crossAxisSpacing:16,
mainAxisSpacing: 16
),
itemCount: 50,
itemBuilder: (context, index) {
return GestureDetector(
onTap: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Verse()),
);
},
child: CircleAvatar(
backgroundColor:Colors.primaries[Random().nextInt(Colors.primaries.length)],
child: Text(index.toString(),style: TextStyle(
fontSize: 20,
color: Colors.white
),),
),
);
},
),
),
);
}
}
答案在这里
static Future<List<Map<String, dynamic>>> gedata() async {
final db = await Dbhelper().db;
return db!.rawQuery('SELECT * from your table name);
}