I am new to dart, and I am working on my first project. I am currently working on the storage aspect of my project, and I have encountered difficulties when trying to access files that have been written in .txt format. I am trying save a file in .txt format which contains a list of lists. I write to file using the .writeAsString() method to convert my list to a string so that it can be saved as a .txt file. I use the following piece of code to read the file and convert it back to a list of lists.

    widget.storage.readData().then((String value) {
    setState(() {
    _state = value;
    final regExp = new RegExp(r'(?:\[)?(\[[^\]]*?\])(?:\])?');

    List<List<String>> result = regExp
        .allMatches(_state)
        .map((m) => m.group(1))
        .map((String item) => item.replaceAll(new RegExp(r'[\[\],]'), ''))
        .map((m) => [m])
        .toList();

    gamesList = result;       

When I print the value of gamesList I am given the following:

    [[75.00% 75.00% No shots 75.00% No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots 75.00% 75.00% No shots 75.00% No shots No shots No shots No shots No shots], [100.00% No shots 100.00% No shots No shots No shots 100.00% No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots No shots 100.00% No shots 100.00% No shots No shots No shots 100.00% No shots No shots]]

While this list appears to hold the same values which I had originally saved, I am not able to access any of these values with an index number. For example, I would like to be able to say:

    print(gamesList[0][1]);

Theoretically this should return the object '75.00%' (refer to the output from the print statement above). I have noticed that there are commas missing from within the list and perhaps this is why my code won't execute the way I would like it to.


Solution 1: Vinicius Pinto

You should use a serialization library in order to convert the Dart object (the list of lists) to a string and then convert it back.

Dartson is an option: https://pub.dartlang.org/packages/dartson

Example:

import 'dart:io';
import 'package:dartson/dartson.dart';

void main() {

  List<String> l1 = new List<String>();
  l1.add("A");
  l1.add("B");

  List<String> l2 = new List<String>();
  l2.add("X");
  l2.add("Y");

  List<List<String>> ll = new List<List<String>>();
  ll.add(l1);
  ll.add(l2);

  var dson = new Dartson.JSON();

  String filename = 'lists.txt';
  new File(filename).writeAsStringSync(dson.encode(ll));

  String s = new File(filename).readAsStringSync();
  List<List<String>> deserializedLists = dson.decode(s, new List<List<String>>(), true);

}