代码之家  ›  专栏  ›  技术社区  ›  ThiagoAM

如何使用Dart/FLIFT在CloudFireStore中存储简单矩阵?

  •  0
  • ThiagoAM  · 技术社区  · 6 年前

    假设在Dart中有以下整数矩阵:

    final List<List<int>> myMatrix = [
      [1, 0, 0],
      [0, 1, 0],
      [0, 0, 1],
    ];
    

    然后我尝试将其存储在Cloud Firestore中,如下所示:

    await Firestore.instance.collection("anyCollection").add({
        "matrix" : myMatrix,
    });
    

    但是,当我运行上面的代码时,模拟器崩溃,我得到以下错误:

    *** First throw call stack:
    (
        0   CoreFoundation                      0x00007fff23c4f02e __exceptionPreprocess + 350
        1   libobjc.A.dylib                     0x00007fff50b97b20 objc_exception_throw + 48
        2   Runner                              0x000000010e83f8b5 _ZN8firebase9firestore4util16ObjcThrowHandlerENS1_13ExceptionTypeEPKcS4_iRKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEE + 581
        3   Runner                              0x000000010e83edf3 _ZN8firebase9firestore4util5ThrowENS1_13ExceptionTypeEPKcS4_iRKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEE + 67
        4   Runner                              0x000000010e890589 _ZN8firebase9firestore4util20ThrowInvalidArgumentIJEEEvPKcDpRKT_ + 57
        5   Runner                              0x000000010e959f65 -[FSTUserDataConverter parseData:context:] + 1045
        6   Runner                              0x000000010e<…>
    Lost connection to device.
    

    我在跑步 cloud_firestore: ^0.13.0+1 pubspec.yaml 文件运行时一切正常 flutter doctor 指挥也是如此。

    0 回复  |  直到 6 年前
        1
  •  2
  •   rsalinas    6 年前

    而您可以在Firestore中存储一个数组,如 documentation ,不允许在其中存储另一个数组。

    您可以采取一种方法,将数组作为字段存储在文档中,这样实际上就可以将每个数组分开。这是显示在这里 thread 或 this one .

        2
  •  0
  •   halfer Jatin Pandey    6 年前

    以下方法适用于您作为示例给出的矩阵,但您无法直接从Firestore查询矩阵。您必须取出整个矩阵,解析它,然后使用它:

    final List<List<int>> myMatrix = [
      [1, 0, 0],
      [0, 1, 0],
      [0, 0, 1],
    ];
    
    // Encode your matrix into a JSON String to add to Firestore
    String jsonMatrix = jsonEncode(myMatrix);
    
    // Decode your Json String into a JSON object
    var decodedMatrix = jsonDecode(jsonMatrix);
    
    // Decode JSON object back into your matrix
    List<List<int>> newList = List<List<int>>.from(decodedMatrix.map((row){
      return List<int>.from(row.map((value) => int.parse(value.toString())));
    }));
    
    // Print to show that the new object is of the same type as the original Matrix
    print(newList is List<List<int>>);
    
        3
  •  0
  •   ThiagoAM    6 年前

    我最终制作了以下帮助程序,将矩阵转换为与Firestore兼容的映射:

    class MatrixHelper {
    
      // Creates a map that can be stored in Firebase from an int matrix.
      static Map<String, dynamic> mapFromIntMatrix(List<List<int>> intMatrix) {
        Map<String, Map<String, dynamic>> map = {};
        int index = 0;
        for (List<int> row in intMatrix) {
          map.addEntries([MapEntry(index.toString(), {})]);
          for (int value in row) {
            map[index.toString()].addEntries(
                [MapEntry(value.toString(), true)]
            );
          }
          index += 1;
        }
        return map;
      }
    
      // Creates an int matrix from a dynamic map.
      static List<List<int>> intMatrixFromMap(Map<dynamic, dynamic> dynamicMap) {
        final map = Map<String, dynamic>.from(dynamicMap);
        List<List<int>> matrix = [];
        map.forEach((stringIndex, value) {
          Map<String, dynamic> rowMap = Map<String, dynamic>.from(value);
          List<int> row = [];
          rowMap.forEach((stringNumber, boolean) {
            row.add(int.parse(stringNumber));
          });
          matrix.add(row);
        });
        return matrix;
      }
    
    }
    

    它的使用非常简单,要在Firestore中保存,它是这样的:

    final List<List<int>> myMatrix = [
      [1, 0, 0],
      [0, 1, 0],
      [0, 0, 1],
    ];
    
    await Firestore.instance.collection("anyCollection").add({
        "matrix" : MatrixHelper.mapFromIntMatrix(myMatrix),
    });
    

    要从Firestore加载矩阵,如下所示:

    // Loads the DocumentSnapshot:
    final document = await Firestore.instance.collection("anyCollection").document("anyDocument").get();
    // Retrieves the Matrix:
    final List<List<int>> matrix = MatrixHelper.intMatrixFromMap(document.data["matrix"]);