Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

flutter - What is the best way to Serialize/Deserialize nested Tuple2 arrays in dart (List<List<Tuple2>>)

I have a nested Tuple2 Array and I am looking for a fast and elegant way to Serialize/Deserialize nested Tuple2 arrays in dart this is the structure: List<List<Tuple2<String,String>>>

[

  [

    [atd, OK],

    [atkw0, ?],

    [atsp0, OK], 

    [ate0, OK], 

    [ati, ELM327v15d], 

    [ath1, OK], 

    [ats0, OK], 

    [atl0, OK],

    [atat1, OK], 

    [0100, 7E9124100FFFFC012], 

    [0120, 7E9124120E01FE01E],

    [0140, 7E91241406FECC078], 

    [0160, 7E912416000000000], 

    [0180, 7E9124180FFFFFFFF], 

    [01A0, 7E91241A0FFFFFFFF], 

    [01C0, NODATA], 

    [0902, 7E98449020031004300340048004A00570043003500300043004C003100320031003200350033],

    [atdp, ISO15765???4(CAN11/250)]],

  [

    [ate0, OK], 

    [ath0, OK], 

    [atal, ?], 

    [ati, ELM327v15d],

    [atl0, OK], 

    [ats0, OK], 

    [atat1, OK], 

    [atcra7E9, ?], 

    [0100, 4100FFFFC012], 

    [0120, 4120E01FE01E], 

    [0140, 41406FECC078], 

    [0160, 416000000000], 

    [0180, 4180FFFFFFFF],

    [01A0, 41A0FFFFFFFF], 

    [01C0, NODATA], 

    [0900, 490040400000], 

    [0902, 49020031004300340048004A00570043003500300043004C003100320031003200350033]

  ], 

  [

    [ate0, OK], 

    [ath0, OK], 

    [atal, ?], 

    [ati, ELM327v15d], 

    [atl0, OK], 

    [ats0, OK], 

    [atat1, OK], 

    [atcra7E9, ?], 

    [0100, 4100FFFFC012], 

    [0120, 4120E01FE01E], 

    [0140, 41406FECC078], 

    [0160, 416000000000], 

    [0180, 4180FFFFFFFF], 

    [01A0, 41A0FFFFFFFF], 

    [01C0, NODATA], 

    [0900, 490040400000], 

    [0902, 49020031004300340048004A00570043003500300043004C003100320031003200350033]

  ], 

  [

    [atdp, ISO15765???4(CAN11/250)]

  ], 

  [

    [010D, 410D6C], 

    [010C, 410C3D8D]

  ], 

  [

    [010D, 410D6C], 

    [010C, 410C3D8D]

  ], 

  [

    [010D, 410D6C], 

    [010C, 410C3D8D]

  ], 

  [

    [010D, 410D6C],

    [010C, 410C3D8D]

  ], 

  [

    [010D, 410D6C], 

    [010C, 410C3D8D]

  ], 

  [

    [010D, 410D6C], 

    [010C, 410C3D8D]

  ], 

  [

    [010D, 410D6C],

    [010C, 410C3D8D], 

    [01A6, 41A6072B]

  ], 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Updated answer.

import 'dart:convert' as convert;

import 'package:tuple/tuple.dart';

class Model {
  final String string1;
  final String string2;

  Model(this.string1, this.string2);

  factory Model.fromTuple(Tuple2 tuple2) {
    final String string1 = tuple2.item1;
    final String string2 = tuple2.item2;
    return Model(string1, string2);
  }

  Tuple2 toTuple() => Tuple2(string1, string2);
}

void main(List<String> arguments) {
  final tuples = <List<Tuple2>>[
    [
      Tuple2('a', 'b'),
      Tuple2('c', 'd'),
      Tuple2('e', 'f'),
    ],
    [
      Tuple2('g', 'h'),
      Tuple2('i', 'j'),
      Tuple2('k', 'l'),
    ],
    [
      Tuple2('m', 'n'),
      Tuple2('o', 'p'),
      Tuple2('q', 'r'),
    ]
  ];

  /// converts tuples to Generic List
  List list = tuples
      .map((e) => e
          .map((e) => {
                'item1': e.item1,
                'item2': e.item2,
              })
          .toList())
      .toList();

  print(tuples is List<List<Tuple2>>);
  print(list is List<List<Tuple2>>);
  print(tuples.length);

  /// converts generic list to json string
  String json = convert.json.encode(list);
  print(json);

  /// converts json string to generic list
  Iterable iterable = convert.json.decode(json);
  List newList = iterable.toList();
  /// converts generic list to List<List<Tuple2>>
  final newTuple = newList
      .map((e) => (e as Iterable)
          .map((e) => Tuple2(
                e['item1'],
                e['item2'],
              ))
          .toList())
      .toList();

  print(newList is List<List<Tuple2>>);
  print(newTuple is List<List<Tuple2>>);
  print(newTuple.length);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...