diff --git a/eight_queens/lib/main.dart b/eight_queens/lib/main.dart index 5a39a4d..b646fb8 100644 --- a/eight_queens/lib/main.dart +++ b/eight_queens/lib/main.dart @@ -68,7 +68,7 @@ class _MyHomePageState extends State { 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 diff --git a/eight_queens/lib/queens.dart b/eight_queens/lib/queens.dart index 6da518c..8448333 100644 --- a/eight_queens/lib/queens.dart +++ b/eight_queens/lib/queens.dart @@ -7,7 +7,7 @@ import 'package:shared_preferences/shared_preferences.dart'; class QueenResolver { - List>> boards = new List(); + int count = 0; Future _isSafe(board, int row, int col, int n) async { // Check row on the left side @@ -43,8 +43,10 @@ class QueenResolver { Future _solveQueens(board, int col, int n) async { if (col >= n) { - _storeSolution(board); - return true; + count++; + _storeSolution(board).then((value) { + return true; + }); } var res = false; @@ -58,7 +60,7 @@ class QueenResolver { return res; } - _storeSolution(List> board) async { + Future _storeSolution(List> board) async { for(int i = 0; i < board.length; i++) { for(int x = 0; x < board[i].length ; x++) { if(board[i][x] == null) { @@ -66,20 +68,11 @@ class QueenResolver { } } } - - boards.add(board); - await _saveToLocalStorage(); - } - - Future _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 solve(int n) async { // Create the initial queens board List> board = new List.generate(n, (_) => new List(n)); diff --git a/eight_queens/lib/results.dart b/eight_queens/lib/results.dart index 0b30432..b166907 100644 --- a/eight_queens/lib/results.dart +++ b/eight_queens/lib/results.dart @@ -12,13 +12,21 @@ class ResultsPage extends StatefulWidget { } class _ResultsPageState extends State { - 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; }); } @@ -26,8 +34,8 @@ class _ResultsPageState extends State { void initState() { retrieveEightQueensResults().then((value) { print('async done'); + super.initState(); }); - super.initState(); } @override @@ -37,7 +45,7 @@ class _ResultsPageState extends State { 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()); },