大家晚上好,我的 SQLite 数据库上出现了一个奇怪的错误,没有这样的表:Stages in 'select * from Stages' 尽管数据库已打开并且有一个附加屏幕显示我在 SQLlite studio 上的查询,并且我有另一个数据库与表同名并且有效!这就是我的代码
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:mo3lmty/Modules/Categories.dart';
import 'package:mo3lmty/Modules/Drawer.dart';
import 'package:mo3lmty/Screens/draw_letters.dart';
import 'package:mo3lmty/Screens/examples_letters.dart';
import 'package:mo3lmty/Screens/expand_list.dart';
import 'package:mo3lmty/Screens/match_screen.dart';
import 'package:mo3lmty/Screens/numbers_screen.dart';
import 'package:mo3lmty/Screens/reading_letters.dart';
import 'package:mo3lmty/Screens/complete.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
// ignore: must_be_immutable
class HomeScreen extends StatefulWidget {
String? category;
String? age;
String? dbName;
String? path;
HomeScreen({super.key, this.category, this.age, this.dbName});
@override
// ignore: library_private_types_in_public_api
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String? database;
late Future<List> futureData;
late String databaseName;
String? databasesPath;
late BannerAd _bannerAd;
InterstitialAd? _interstitialAd;
bool isloaded = false;
void loadinterad() {
InterstitialAd.load(
adUnitId: "ca-app-pub-2483803582097264/4165779011",
request: const AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
// Keep a reference to the ad so you can show it later.
_interstitialAd = ad;
},
onAdFailedToLoad: (LoadAdError e) {
"Error {$e}";
},
));
}
void showInterad() {
if (_interstitialAd == null) {
return;
}
_interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (InterstitialAd ad) {},
onAdDismissedFullScreenContent: (InterstitialAd ad) {
ad.dispose();
},
onAdFailedToShowFullScreenContent:
(InterstitialAd ad, AdError aderror) {
ad.dispose();
loadinterad();
});
_interstitialAd!.show();
_interstitialAd = null;
}
loadbanner() {
_bannerAd = BannerAd(
size: AdSize.banner,
adUnitId: "ca-app-pub-2483803582097264/5670432370",
listener: BannerAdListener(
onAdLoaded: (ad) {
setState(() {
isloaded = true;
});
},
onAdFailedToLoad: (ad, error) {}),
request: const AdRequest());
_bannerAd.load();
}
@override
void initState() {
database = "${widget.age!}_${widget.dbName!}";
prepareDatabase();
futureData = fetchData();
loadbanner();
showInterad();
loadinterad();
super.initState();
}
@override
void dispose() {
// Release decoders and buffers back to the operating system making them
// available for other apps to use.
_bannerAd.dispose();
_interstitialAd!.dispose();
super.dispose();
}
Future prepareDatabase() async {
widget.path = join('assets/Thumbs/', '$database.db');
try {
WidgetsFlutterBinding.ensureInitialized();
databaseName = '$database.db';
databasesPath = await getDatabasesPath();
//get DB from assets and load it
var dbDir = await getDatabasesPath();
var dbPath = join(dbDir, "$database.db");
if (File(dbPath).existsSync()) {
} else {
ByteData data = await rootBundle.load('assets/Thumbs/$database.db');
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(dbPath).writeAsBytes(bytes);
}
await fetchData();
} on Exception catch (e) {
"Error {$e}";
}
}
Future<List> fetchData() async {
var dbDir = await getDatabasesPath();
var dbPath = join(dbDir, "$database.db");
final db = await openDatabase(dbPath, version: 2);
return db.rawQuery('select * from Stages');
}
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(
widget.category.toString(),
style: const TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
fontFamily: 'El Messiri'),
),
],
),
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
Color.fromRGBO(78, 84, 200, 1.0),
Color.fromRGBO(127, 133, 239, 1.0),
]),
),
),
),
drawer: const DrawerWidget(),
bottomNavigationBar: isloaded
? Container(
alignment: Alignment.center,
width: _bannerAd.size.width.toDouble(),
height: _bannerAd.size.height.toDouble(),
child: AdWidget(
ad: _bannerAd,
),
)
: const SizedBox(),
body: FutureBuilder(
future: futureData,
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
} else {
return GridView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: snapshot.data.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0),
itemBuilder: (BuildContext context, int i) {
Data course = Data.fromMap(snapshot.data[i]);
return Container(
height: size.height,
width: size.width,
margin: const EdgeInsets.only(
left: 12.0, right: 12.0, top: 10.0, bottom: 10.0),
child: Card(
shadowColor: Colors.blue[900],
elevation: 8.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: GestureDetector(
onTap: () {
if (i == 0) {
if (database == '3_Arabic') {
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Reading_Letters",
hassong: true,
));
}
if (database == '4_Arabic') {
showInterad();
loadinterad();
Get.to(() => DrawLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Writting_Letters"));
}
}
if (i == 1) {
if (database == '3_Arabic') {
showInterad();
loadinterad();
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Numbers",
hassong: true,
));
}
if (database == '4_Arabic') {
Get.to(() => DrawLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Writing_Numbers"));
}
}
if (i == 2) {
if (database == '3_Arabic') {
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Colors",
hassong: true,
));
}
if (database == '4_Arabic') {
Get.to(() => Examples(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Examples_Letters"));
}
}
if (i == 3) {
showInterad();
loadinterad();
if (database == '3_Arabic') {
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Shapes",
hassong: true,
));
}
if (database == '4_Arabic') {
Get.to(() => MatchScreen(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Match",
));
}
}
if (i == 4) {
if (database == '3_Arabic') {
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Fruits",
hassong: true,
));
}
if (database == '4_Arabic') {
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Tongue",
hassong: false,
));
}
}
if (i == 5) {
if (database == '3_Arabic') {
Get.to(() => Numbers(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Animal_Stages",
));
}
if (database == '4_Arabic') {
Get.to(() => ExpandList(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "",
));
}
}
if (i == 6) {
if (database == '3_Arabic') {
showInterad();
loadinterad();
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Food_Tools",
hassong: false));
}
if (database == '4_Arabic') {
Get.to(() => CompleteWords(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "choose",
));
}
}
if (i == 7) {
showInterad();
loadinterad();
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "vegetables",
hassong: true));
}
if (i == 8) {
showInterad();
loadinterad();
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Clothes",
hassong: false));
}
if (i == 9) {
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Body",
hassong: false));
}
if (i == 10) {
showInterad();
loadinterad();
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Jobs",
hassong: true));
}
if (i == 11) {
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Weather",
hassong: true));
}
if (i == 12) {
showInterad();
loadinterad();
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Transportations",
hassong: true));
}
if (i == 13) {
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Opposites",
hassong: true));
}
if (i == 14) {
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Electrical",
hassong: false));
}
if (i == 15) {
showInterad();
loadinterad();
Get.to(() => ReadingLetters(
category: course.Stage_Name.toString(),
age: widget.age,
dbName: widget.dbName,
tableName: "Furniture",
hassong: true));
}
},
child: Container(
// height: 150,
// width: 150,
alignment: Alignment.bottomLeft,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(course.Image_Name.toString()),
fit: BoxFit.fitWidth,
alignment: Alignment.topCenter,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
IconButton(
onPressed: () {},
alignment: Alignment.centerLeft,
icon: const Icon(
Icons.favorite,
size: 20.5,
color: Color.fromRGBO(78, 84, 200, 1.0),
),
),
Flexible(
child: Container(
padding: const EdgeInsets.only(
right: 8.0, top: 4.0),
child: Text(
course.Stage_Name.toString(),
textAlign: TextAlign.center,
overflow: TextOverflow.clip,
style: const TextStyle(
fontSize: 13.5,
color: Colors.black,
fontFamily: 'El Messiri'),
),
),
),
],
),
const LinearProgressIndicator(
value: 1.0,
valueColor: AlwaysStoppedAnimation<Color>(
Color.fromRGBO(78, 84, 200, 1.0)),
minHeight: 12.0,
),
],
),
),
),
),
);
},
);
}
}));
}
}
我现在不知道出了什么问题,但我将版本降级为 sqflite:^2.0.2 而不是 2.3.2 并且它有效!