Home > Net >  why .take() didn't take the specific number of items with flutter?
why .take() didn't take the specific number of items with flutter?

Time:11-11

I have a list that I extract it into sublists and then I splitted it into sublists of 20 items. Now I want to take juste 200 items of this list and then split it But .take didn't work for me.

 static Future<List> localPath() async {
    File textAsset = File('/storage/emulated/0/RPSApp/assets/bluetooth.txt');
    final text = await textAsset.readAsString();
    final bytes = text
        .split(',')
        .map((s) => s.trim())
     
        .map((s) => int.parse(s))
        .toList();

    int chunkSize = 20;
    final dd = bytes.take(200);
    print("BYTES : $dd");
    List<int> padTo(List<int> input, int count) {
      return [...input, ...List.filled(count - input.length, 255)];
    }

    List<int> padToChunksize(List<int> input) =>
        padTo(input.toList(), chunkSize);
    final items =
        bytes.slices(chunkSize).map(padToChunksize).take(200).toList();

    return items;
    //return items;
  }

even when I tried the code below I didn't get the expected output

return items.take(200).toList();

this is how I use the function:

    final chunks = await Utils.localPath();

 await Future.forEach(chunks, (chunk) async {
      final d = chunk as List<int>;
      await c.write(d, withoutResponse: true);
      await c.read();
      await Future.delayed(const Duration(seconds: 4));
    });

CodePudding user response:

List<T>.take() return Iterable<T>

so if you want to return it as List call toList()

return items.take(200).toList();

CodePudding user response:

items.expand((e) => e).take(200).toList();
  • Related