Skip to content

Commit

Permalink
Hot fixed storage issue
Browse files Browse the repository at this point in the history
Futures and Async code is hard
  • Loading branch information
martin-headspace committed Feb 17, 2020
1 parent 8deaa66 commit d2036ea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion eight_queens/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class _MyHomePageState extends State<MyHomePage> {
await QueenResolver().solve(_boardSize);
pr.dismiss();

Navigator.push(context, MaterialPageRoute(builder: (context) => ResultsPage(title: "Results")));
await Navigator.push(context, MaterialPageRoute(builder: (context) => ResultsPage(title: "Results")));
}

@override
Expand Down
23 changes: 8 additions & 15 deletions eight_queens/lib/queens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:shared_preferences/shared_preferences.dart';

class QueenResolver {

List<List<List<int>>> boards = new List();
int count = 0;

Future<bool> _isSafe(board, int row, int col, int n) async {
// Check row on the left side
Expand Down Expand Up @@ -43,8 +43,10 @@ class QueenResolver {
Future<bool> _solveQueens(board, int col, int n) async {

if (col >= n) {
_storeSolution(board);
return true;
count++;
_storeSolution(board).then((value) {
return true;
});
}

var res = false;
Expand All @@ -58,28 +60,19 @@ class QueenResolver {
return res;
}

_storeSolution(List<List<int>> board) async {
Future _storeSolution(List<List<int>> board) async {
for(int i = 0; i < board.length; i++) {
for(int x = 0; x < board[i].length ; x++) {
if(board[i][x] == null) {
board[i][x] = 0;
}
}
}

boards.add(board);
await _saveToLocalStorage();
}

Future<bool> _saveToLocalStorage() async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
prefs.setString('result', boards.toString());
await prefs.setString('board'+count.toString(), board.toString());
await prefs.setInt('count', count);
}




Future<bool> solve(int n) async {
// Create the initial queens board
List<List<int>> board = new List.generate(n, (_) => new List(n));
Expand Down
18 changes: 13 additions & 5 deletions eight_queens/lib/results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@ class ResultsPage extends StatefulWidget {
}

class _ResultsPageState extends State<ResultsPage> {
var boards;
var boards = new List();

Future retrieveEightQueensResults() async {
final prefs = await SharedPreferences.getInstance();
String boardsString = await prefs.get('result');
int count = await prefs.getInt("count");
print(count);
var _boards = new List();
for(int i = 1 ; i < count + 1; i++) {
String s_board = prefs.getString('board'+i.toString());
var board = jsonDecode(s_board);
_boards.add(board);
}

setState(() {
boards = jsonDecode(boardsString);
boards = _boards;
});
}

@override
void initState() {
retrieveEightQueensResults().then((value) {
print('async done');
super.initState();
});
super.initState();
}

@override
Expand All @@ -37,7 +45,7 @@ class _ResultsPageState extends State<ResultsPage> {
title: Text(widget.title),
),
body: new ListView.builder(
itemCount: boards.length,
itemCount: boards.length ?? 0,
itemBuilder: (BuildContext context, int index) {
return new Text(boards[index].toString());
},
Expand Down

0 comments on commit d2036ea

Please sign in to comment.