en fait regarde :
import 'package:flutter/material.dart';
import 'package:mobile_tp/models/Difficulte.dart';
import 'package:mobile_tp/models/Niveau.dart';
import 'package:mobile_tp/models/Partie.dart';
import 'package:mobile_tp/screens/MysteryNumberScreen.dart';
import 'package:mobile_tp/services/AventureDB.dart';
import 'package:mobile_tp/services/DifficulteDB.dart';
import 'package:mobile_tp/services/EffectuerDB.dart';
import 'package:mobile_tp/services/NiveauDB.dart';
import 'package:mobile_tp/services/PartieDB.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:math';
class PageNiveaux extends StatefulWidget {
final int totalNiveaux;
final int idAventure;
final Future database;
PageNiveaux({
Key? key,
required this.totalNiveaux,
required this.idAventure,
required this.database,
}) : super(key: key);
@override
_PageNiveauxState createState() => _PageNiveauxState();
}
class _PageNiveauxState extends State {
late List doneLevels = [];
late AventureDB aventureDB;
late PartieDB partieDB;
late EffectuerDB effectuerDB;
late NiveauDB niveauDB;
late DifficulteDB difficulteDB;
@override
void initState() {
super.initState();
aventureDB = AventureDB(database: widget.database);
partieDB = PartieDB(database: widget.database);
effectuerDB = EffectuerDB(database: widget.database);
niveauDB = NiveauDB(database: widget.database);
difficulteDB = DifficulteDB(database: widget.database);
loadLevels();
}
Future loadLevels() async {
final levels = await aventureDB.getLevelsWon(widget.idAventure);
setState(() {
doneLevels = levels;
print("doneLevels : $doneLevels");
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Aventure'),
),
body: Center(
child: SingleChildScrollView(
child: Column(
children: List.generate(widget.totalNiveaux * 2 - 1, (index) {
if (index % 2 == 0) {
int niveau = widget.totalNiveaux - index ~/ 2;
bool niveauGagne = doneLevels
.any((niveauGagne) => niveauGagne.palier == niveau);
bool niveauPrecedentGagne = niveau == 1 ||
doneLevels
.any((niveauGagne) => niveauGagne.palier == niveau - 1);
print(
'niveauGagne: niveauGagne,niveauPrecedentGagne:niveauPrecedentGagne');
return GestureDetector(
onTap: niveauPrecedentGagne
? () async {
int idDifficulte;
if (niveau <= 3) {
idDifficulte = 1;
} else if (niveau <= 7) {
idDifficulte = 2;
} else {
idDifficulte = 3;
}
Difficulte difficulte =
await niveauDB.getDifficulteById(idDifficulte);
int mysteryNumber =
Random().nextInt(difficulte.valeurMax) + 1;
Partie nouvellePartie = Partie(
score: 0,
nbMystere: mysteryNumber,
nbEssaisJoueur: difficulte.nbTentatives,
gagne: false,
dateDebut: DateTime.now(),
dateFin: DateTime.now(),
idAventure: widget.idAventure,
idNiveau: niveau,
);
int idNouvellePartie =
await partieDB.add(nouvellePartie);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MysteryNumberScreen(
database: widget.database,
idPartie: idNouvellePartie,
idNiveau: niveau,
idDifficulte: difficulte.id,
idAventure: widget.idAventure,
effectuerDB: effectuerDB,
onPartieFinished: loadLevels,
),
),
);
List allParties = await partieDB.getAll();
print(allParties[allParties.length - 1].toString());
}
: null,
child: CircleAvatar(
backgroundColor: niveauGagne ? Colors.green : Colors.grey,
child: Text(niveau.toString()),
),
);
} else {
return SizedBox(height: 20);
}
}),
),
),
),
);
}
}
import 'package:mobile_tp/models/Aventure.dart';
import 'package:mobile_tp/models/Niveau.dart';
import 'package:mobile_tp/models/Partie.dart';
import 'package:sqflite/sqflite.dart';
class AventureDB {
final Future database;
AventureDB({required this.database});
Future add(String nomJoueur) async {
final db = await database;
return await db.rawInsert(
'INSERT INTO Aventure(nomJoueur) VALUES(?)',
[nomJoueur],
);
}
Future<List> getAll() async {
final db = await database;
final List<Map<String, Object?>> aventureMaps = await db.query('Aventure');
return [
for (final {
'idAventure': id as int,
'nomJoueur': nom as String,
} in aventureMaps)
Aventure(id: id, nomJoueur: nom)
];
}
Future<List> getLevelsWon(int idAventure) async {
final db = await database;
final List<Map<String, Object?>> levelsMaps = await db.rawQuery('''
SELECT Niveau.idNiveau, Niveau.palier, Niveau.idDifficulte
FROM Niveau
JOIN Effectuer ON Effectuer.idNiveau = Niveau.idNiveau
JOIN Partie ON Partie.idPartie = Effectuer.idPartie
WHERE Effectuer.idAventure = ? AND Effectuer.complete = 1 AND Partie.gagne = 1
''', [idAventure]);
print(levelsMaps);
return [
for (final {
'idNiveau': id as int,
'palier': palier as int,
'idDifficulte': idDifficule as int,
} in levelsMaps)
Niveau(id: id, palier: palier, idDifficulte: idDifficule)
];
}
Future<List> getLevelsOfAdventure(int idAventure) async {
final db = await database;
final List<Map<String, Object?>> levelsMaps = await db.rawQuery('''
SELECT Niveau.idNiveau, Niveau.palier
FROM Niveau
JOIN Partie ON Partie.idNiveau = Niveau.idNiveau
WHERE Partie.idAventure = ?
''', [idAventure]);
return [
for (final {
'idNiveau': id as int,
'palier': palier as int,
'idDifficulte': idDifficule as int,
} in levelsMaps)
Niveau(id: id, palier: palier, idDifficulte: idDifficule)
];
}
}
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class SqliteService {
Future onCreate(Database db, int version) async {
await db.execute("""
CREATE TABLE Aventure (
idAventure INTEGER PRIMARY KEY AUTOINCREMENT,
nomJoueur TEXT NOT NULL
)
""");
await db.execute("""
CREATE TABLE Difficulte (
idDifficulte INTEGER PRIMARY KEY AUTOINCREMENT,
nom TEXT NOT NULL,
nbTentative INTEGER NOT NULL,
valeurMax INTEGER NOT NULL
)
""");
await db.execute("""
CREATE TABLE Niveau (
idNiveau INTEGER PRIMARY KEY AUTOINCREMENT,
palier INTEGER NOT NULL,
idDifficulte INTEGER NOT NULL,
FOREIGN KEY(idDifficulte) REFERENCES Difficulte(idDifficulte)
)
""");
await db.execute("""
CREATE TABLE Partie (
idPartie INTEGER PRIMARY KEY AUTOINCREMENT,
score INTEGER NOT NULL,
nbMystere INTEGER NOT NULL,
nbEssaisJoueur INTEGER NOT NULL,
gagne INTEGER NOT NULL,
dateDebut TEXT NOT NULL,
dateFin TEXT NOT NULL,
idAventure INTEGER,
idNiveau INTEGER,
FOREIGN KEY(idAventure) REFERENCES Aventure(idAventure),
FOREIGN KEY(idNiveau) REFERENCES Niveau(idNiveau)
)
""");
await db.execute("""
CREATE TABLE Effectuer (
idAventure INTEGER NOT NULL,
idPartie INTEGER NOT NULL,
idNiveau INTEGER NOT NULL,
complete INTEGER NOT NULL,
PRIMARY KEY(idAventure, idPartie, idNiveau),
FOREIGN KEY(idAventure) REFERENCES Aventure(idAventure),
FOREIGN KEY(idPartie) REFERENCES Partie(idPartie),
FOREIGN KEY(idNiveau) REFERENCES Niveau(idNiveau)
)
""");
}
Future initializeDB() async {
return openDatabase(
join(await getDatabasesPath(), 'aventure.db'),
onCreate: onCreate,
onUpgrade: (db, oldVersion, newVersion) async {
await db.execute("""
DROP TABLE IF EXISTS Aventure;
DROP TABLE IF EXISTS Difficulte;
DROP TABLE IF EXISTS Niveau;
DROP TABLE IF EXISTS Partie;
DROP TABLE IF EXISTS Effectuer;
""");
await onCreate(db, newVersion);
},
version: 2,
);
}
}
quand je fais une partie et que je la gagne, ça me print ça :
Performing hot restart...
Restarted application in 1 211ms.
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/chatty ( 3878): uid=10155(com.example.mobile_tp) 1.ui identical 7 lines
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: true
I/flutter ( 3878): []
I/flutter ( 3878): doneLevels : []
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/chatty ( 3878): uid=10155(com.example.mobile_tp) 1.ui identical 7 lines
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: true
I/flutter ( 3878): Partie{id: 38, score: 0, nbMystere: 20, nbEssaisJoueur: 20, gagne: false, dateDebut: 2024-03-31 12:02:53.861259, dateFin: 2024-03-31 12:02:53.861272, idAventure: 1, idNiveau: 1}
I/AssistStructure( 3878): Flattened final assist data: 388 bytes, containing 1 windows, 3 views
D/InputConnectionAdaptor( 3878): The input method toggled cursor monitoring on
I/flutter ( 3878): Adding Effectuer with values: {idAventure: 1, idNiveau: 1, idPartie: 38, complete: 1}
I/flutter ( 3878): Updating Partie with values: {idPartie: 38, score: 0, nbMystere: 20, nbEssaisJoueur: 20, gagne: 1, dateDebut: 2024-03-31T12:02:53.861259, dateFin: 2024-03-31T12:02:53.861272, idAventure: 1, idNiveau: 1}
I/flutter ( 3878): []
I/flutter ( 3878): doneLevels : []
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/chatty ( 3878): uid=10155(com.example.mobile_tp) 1.ui identical 7 lines
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: true
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/chatty ( 3878): uid=10155(com.example.mobile_tp) 1.ui identical 7 lines
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: true
I/flutter ( 3878): []
I/flutter ( 3878): doneLevels : []
D/InputConnectionAdaptor( 3878): The input method toggled cursor monitoring off
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/chatty ( 3878): uid=10155(com.example.mobile_tp) 1.ui identical 7 lines
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: false
I/flutter ( 3878): niveauGagne: false, niveauPrecedentGagne: true
donc je ne sais pas si c'est ma requête qui ne va pas ou ma bd car doneLevels est toujours vide