i'm new in flutter , i'm looking to create a score screen which ranks people according to their score in my flutter game but for some reason, "no scores yet" keep being displayed instead of the scores......... please check my code and let me know if there is something i can add to see the score screen
class ScoreScreen extends StatelessWidget {
dynamic query;
ScoreScreen({this.query});
List<TableRow> createRow(dynamic query) {
query.sort((dynamic a, dynamic b) => b.toString().compareTo(a.toString()));
List<TableRow> rows = [];
rows.add(
TableRow(
children: [
Padding(
padding: const EdgeInsets.only(bottom: 15.0),
child: Center(
child: Text(
"Rank",
style: kHighScoreTableHeaders,
),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 15.0),
child: Center(
child: Text(
"Date",
style: kHighScoreTableHeaders,
),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 15.0),
child: Center(
child: Text(
"Score",
style: kHighScoreTableHeaders,
),
),
),
],
),
);
print("${query[0]} this is query 0");
int numOfRows = query.length;
List<String> topRanks = ["", "", ""];
for (var i = 0; i < numOfRows && i < 10; i++) {
var row = query[i].toString().split(",");
var date = row[1].split(" ")[0].split("-");
var scoreDate = formatDate(
DateTime(int.parse(date[0]), int.parse(date[1]), int.parse(date[2])),
[yy, '-', M, '-', d]);
Widget item = TableCell(
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(
i < 3 ? topRanks[i] + '${i + 1}' : '${i + 1}',
style: kHighScoreTableRowsStyle,
textAlign: TextAlign.center,
),
),
);
Widget item1 = TableCell(
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
'$scoreDate',
style: kHighScoreTableRowsStyle,
textAlign: TextAlign.center,
),
),
),
);
Widget item2 = TableCell(
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(
'${row[0]}',
style: kHighScoreTableRowsStyle,
textAlign: TextAlign.center,
),
),
);
rows.add(
TableRow(
children: [item, item1, item2],
),
);
}
return rows;}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: query.length == 0
? Stack(crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Center(
child: Text(
"No Scores Yet!",
style: TextStyle(
fontSize: 30.0,
color: Colors.white,
),
),
),
Container(
padding: EdgeInsets.fromLTRB(6.0, 10.0, 6.0, 15.0),
alignment: Alignment.topLeft,
child: IconButton(
tooltip: 'Home',
iconSize: 35,
icon: Icon(MdiIcons.home),
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
onPressed: () {
Navigator.pop(context);
},
),
),
],
)
: Column(
children: <Widget>[
Stack(
alignment: AlignmentDirectional.bottomCenter,
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(6.0, 10.0, 6.0, 15.0),
alignment: Alignment.centerLeft,
child: IconButton(
tooltip: 'Home',
iconSize: 35,
icon: Icon(MdiIcons.home),
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
onPressed: () {
Navigator.pop(context);
},
),
),
Center(
child: Container(
margin: EdgeInsets.fromLTRB(8.0, 10.0, 8.0, 15.0),
child: Text(
'High Scores',
style: TextStyle(
color: Colors.white,
fontSize: 45.0,
fontWeight: FontWeight.w300,
),
),
),
),
],
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Table(
defaultVerticalAlignment:
TableCellVerticalAlignment.middle,
textBaseline: TextBaseline.alphabetic,
children: createRow(query),
),
),
),
],
),
),
);
} }
class LoadingScreen extends StatefulWidget {
@override
LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
@override
void initState() {
super.initState();
queryScores();
}
void queryScores() async {
final database = score_database.openDB();
var queryResult = await score_database.scores(database);
Navigator.pushReplacement<dynamic,dynamic>(
context,
MaterialPageRoute<dynamic>(
builder: (context) {
return ScoreScreen(
query: queryResult,
);
},
),
);
}
Future<Database> openDB() async {
final database = openDatabase(
join(await getDatabasesPath(), 'scores_database.db'),
onCreate: (db, version) {
return db.execute(
"CREATE TABLE scores(id INTEGER PRIMARY KEY AUTOINCREMENT, scoreDate
TEXT, userScore INTEGER)",
);
},
version: 1,
);
return database;
}
Future<void> insertScore(Score score, dynamic database) async {
final Database db = await database;
await db.insert(
'scores',
score.toMap(),
conflictAlgorithm: ConflictAlgorithm.ignore,
);
}
Future<List<Score>> scores(dynamic database) async {
// Get a reference to the database.
final Database db = await database;
// Query the table for all The Dogs.
final List<Map<String, dynamic>> maps = await db.query('scores');
// Convert the List<Map<String, dynamic> into a List<Dog>.
return List.generate(maps.length, (i) {
return Score(
id: maps[i]['id'],
scoreDate: maps[i]['scoreDate'],
userScore: maps[i]['userScore'],
);
});
}
Future<void> updateScore(Score score, dynamic database) async {
// Get a reference to the database.
dynamic db = await database;
// Update the given Dog.
await db.update(
'scores',
score.toMap(),
// Ensure that the Dog has a matching id.
where: "id = ?",
// Pass the Dog's id as a whereArg to prevent SQL injection.
whereArgs: <dynamic>[score.id],
);
}
Future<void> deleteScore(int id, dynamic database) async {
// Get a reference to the database.
dynamic db = await database;
// Remove the Dog from the database.
await db.delete(
'scores',
// Use a `where` clause to delete a specific dog.
where: "id = ?",
// Pass the Dog's id as a whereArg to prevent SQL injection.
whereArgs: [id],
);
}
void manipulateDatabase(Score scoreObject, dynamic database) async {
await insertScore(scoreObject, database);
print(await scores(database));
}
class Score {
final dynamic id;
final dynamic scoreDate;
final dynamic userScore;
Score({this.id, this.scoreDate, this.userScore});
Map<String, int> toMap() {
return {
//'id': id,
'scoreDate': scoreDate,
'userScore': userScore,
};
}
// Implement toString to make it easier to see information about
// each dog when using the print statement.
@override
String toString() {
return '$userScore,$scoreDate,$id';
}
}