I have exercises stored in my sqflite database as strings. I would like to retrieve the data and put it into a list or array called typeArray[]. I am trying to retrieve the data in a class that will use the list of exercises in functions to perform random workout generation. Therefore I need to retrieve the list before doing the calculations. The return values of these workout generator functions is then passed using MaterialPageRoute() to custom_workout_screen.dart where I use the return values of the functions to be displayed as strings to the screen.
I got it to work with predefined list ie where I had set the list = ['exerciseOne', 'exerciseTwo', 'etc']
How would I pull the data in a class that is not stateless or statefull? I am happy to do the work, but am confused in terms of the approach to the problem - any guidance appreciated!
Generates Custom Workout to be passed to the Custom Workout Screen: generate_custom.dart
class GenerateCustom {
int rnd;
// NEED TO SET typeArray to values from SQFLITE DB ?
List typeArray = [];
GenerateCustom({this.difficulty});
final int difficulty;
String workout;
String ex1;
String ex2;
String ex3;
String ex4;
String ex5;
String getStrengthType() {
var random = Random();
var i = random.nextInt(14);
print(typeArray[i]);
return typeArray[i];
}
String cuExerciseOne() {
if (difficulty == 1) {
workout = ('1: ' +
getStrengthType() +
' ' +
getRepsEasy() +
'x' +
getSetsEasy());
} else if (difficulty == 2) {
workout = ('1: ' +
getStrengthType() +
' ' +
getRepsMedium() +
'x' +
getSetsMedium());
} else {
workout = ('1: ' +
getStrengthType() +
' ' +
getRepsHard() +
'x' +
getSetsHard());
}
return workout;
}
DatabaseHelper: database_helper.dart
class DatabaseHelper {
static DatabaseHelper _databaseHelper;
static Database _database;
String exerciseTable = 'exercise_table';
String colId = 'id';
String title = 'title';
DatabaseHelper._createInstance();
factory DatabaseHelper() {
if (_databaseHelper == null) {
_databaseHelper = DatabaseHelper._createInstance();
}
return _databaseHelper;
}
Future<Database> get database async {
if (_database == null) {
_database = await initializeDatabase();
}
return _database;
}
Future<Database> initializeDatabase() async {
//get ios + android dir path
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + 'exercise.db';
//Open or create db at path
var exerciseDatabase =
await openDatabase(path, version: 1, onCreate: _createDb);
return exerciseDatabase;
}
void _createDb(Database db, int newVersion) async {
await db.execute(
'CREATE TABLE $exerciseTable ($colId INTEGER PRIMARY KEY, $title TEXT)');
}
Future<List<Map<String, dynamic>>> getExerciseMapList() async {
Database db = await this.database;
var result = await db.rawQuery('SELECT * FROM $exerciseTable');
return result;
}
//Insert
Future<int> insertExercise(Exercise exercise) async {
Database db = await this.database;
var result = await db.insert(exerciseTable, exercise.toMap());
return result;
}
//Update
Future<int> updateExercise(Exercise exercise) async {
var db = await this.database;
var result = await db.update(exerciseTable, exercise.toMap(),
where: '$colId = ?', whereArgs: [exercise.id]);
debugPrint('update called');
return result;
}
//Delete
Future<int> deleteExercise(int id) async {
var db = await this.database;
int result =
await db.rawDelete('DELETE FROM $exerciseTable WHERE $colId = $id');
return result;
}
//get no of objects in db
Future<int> getCount() async {
Database db = await this.database;
List<Map<String, dynamic>> x =
await db.rawQuery('SELECT COUNT (*) from $exerciseTable');
int result = Sqflite.firstIntValue(x);
return result;
}
//get Map list from db, convert to Exercise List object
Future<List<Exercise>> getExerciseList() async {
//get map list and # of entries in db
var exerciseMapList = await getExerciseMapList();
int count = exerciseMapList.length;
List<Exercise> exerciseList = List<Exercise>();
//Loop to create exercise list from a map list
for (int i = 0; i < count; i++) {
exerciseList.add(Exercise.fromMapObject(exerciseMapList[i]));
}
return exerciseList;
}
}
Screen which uses output: custom_workout_screen.dart
class CustomWorkoutScreen extends StatelessWidget {
CustomWorkoutScreen(
this.customDifficulty,
this.customWorkout1,
this.customWorkout2,
this.customWorkout3,
this.customWorkout4,
this.customWorkout5);
final String customDifficulty;
final String customWorkout1;
final String customWorkout2;
final String customWorkout3;
final String customWorkout4;
final String customWorkout5;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: kBackgroundColour,
appBar: AppBar(
backgroundColor: kButtonAndBarColour,
title: Text('CUSTOM', style: kTitleStyle),
),
body: Center(
child: Column(
children: <Widget>[
SizedBox(height: 15.0),
Expanded(
child: Text(
'WORKOUT',
style: kTitleStyleDark,
),
),
Container(
child: Text(
'DIFFICULTY: ' + customDifficulty,
style: kTitleStyleDark,
),
),
SizedBox(
height: 25.0,
),
Container(
margin: EdgeInsets.only(bottom: 10.0),
child: Column(
children: <Widget>[
Text('READY...GO!', style: kCommentTextStyle),
Text(
customWorkout1,
style: kMainTextStyledDark,
),
Text('REST 30s!', style: kCommentTextStyle),
Text(
customWorkout2,
style: kMainTextStyledDark,
),
Text('REST 30s!', style: kCommentTextStyle),
Text(
customWorkout3,
style: kMainTextStyledDark,
),
Text('REST 30s!', style: kCommentTextStyle),
Text(
customWorkout4,
style: kMainTextStyledDark,
),
Text('REST 30s!', style: kCommentTextStyle),
Text(
customWorkout5,
style: kMainTextStyledDark,
),
Text('GOOD JOB, REST!', style: kCommentTextStyle),
Text('OR GO AGAIN?', style: kCommentTextStyle),
],
),
),
SizedBox(
height: 50.0,
),
BottomButton(
buttonTitle: 'NEW WORKOUT',
onTap: () {
Navigator.pop(context);
},
),
],
),
),
);
}
}