Repository: flutter
Issue: Stream.fromIterable(things).asyncMap(convert) Missing elements in the list.
Labels: framework,has reproducible steps,P2,found in release: 3.0,found in release: 3.1,team-framework,triaged-framework

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

| Info                           | Value                                                 |      |
| ------------------------------ | ----------------------------------------------------- | ---- |
| Platform Name                  | flutter|      |
| Platform Version               | 3.0.5                              |      |
| Xcode Version | Xcode 13.2.1              |      |
| Demo project link              | [link to the demo project that highlights the issue](https://github.com/Mo5hu/FlutterIssue-AsyncMap-) |      |

### Issue Description and Steps

I am trying to use a Stream.fromIterable(things).asyncMap(convert); 

```
Future<TouchPoints?> convert(TouchPoints? thing) async {
    await Future.delayed(const Duration(milliseconds: 2));
    // changing the delay for function does affect the
    // accuracy of painter results.
    // set this to 5 milliseconds and then there will be a smooth line, which is not required here.
    // set this to 25 milliseconds, or more, and then there will be a dotted line of x = y.

    return thing;
  }

  Stream<TouchPoints?> doStuff(List<TouchPoints?> things) {
    return Stream.fromIterable(things).asyncMap(convert);
    // This asyncMap seems to be the culprit here, My Guess!...
  }
```

This returned stream is used to stream points, used along with a custom painter.

**Expected results:** 
When used with `Future.delayed(const Duration(milliseconds: 25));`, or greater than 25 milliseconds, the results are as expected. See picture below:

<img width="1250" alt="Screenshot 2022-08-11 at 6 01 24 AM" src="https://user-images.githubusercontent.com/42168933/184047825-84c76773-4ae8-427f-bc5e-425ab4de7784.png">


**Actual results:** 
The issue here is that with `Future.delayed(const Duration(milliseconds: 2));`, stream `snapshot.data` doesn't returns the complete array thus we see results like:
 
<img width="1254" alt="Screenshot 2022-08-11 at 6 00 31 AM" src="https://user-images.githubusercontent.com/42168933/184047767-834bf525-9cb2-4106-91d6-e63a009a069b.png">

Note*: Here, i am guessing asyncMap is missing the data, or might be some other issue.

<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```
import 'dart:async';

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);

  List<TouchPoints?> records = [];

  @override
  Widget build(BuildContext context) {
    for (int i = 0; i < 25; i++) {
      records.add(TouchPoints(
          points: Offset((i + 1) * 5, (i + 1) * 5),
          paint: Paint()
            ..strokeCap = StrokeCap.round
            ..color = Colors.red
            ..strokeWidth = 5));
      records.add(null);
      // adding null points after each point to make a dotted line.
    }
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: DrawingWidget(rec: records),
    );
  }
}

class DrawingWidget extends StatefulWidget {
  const DrawingWidget({Key? key, required this.rec}) : super(key: key);

  final List<TouchPoints?> rec;

  @override
  State<DrawingWidget> createState() => _DrawingWidgetState();
}

class _DrawingWidgetState extends State<DrawingWidget> {
  _DrawingWidgetState();
  List<TouchPoints?> drawPoints = [];

  @override
  Widget build(BuildContext context) {
    print('Building drawing widget');
    drawPoints = [];

    return SafeArea(
      child: Scaffold(
          body: SizedBox.expand(
              child: ClipRRect(
                  borderRadius: const BorderRadius.all(Radius.circular(20)),
                  child: StreamBuilder<TouchPoints?>(
                      stream: doStuff(widget.rec),
                      builder: (context, snapshot) {
                        if (snapshot.connectionState ==
                            ConnectionState.active) {
                          drawPoints.add(snapshot.data);
                        }
                        return CustomPaint(
                            size: Size.infinite,
                            painter: SignaturePainter(
                              points: drawPoints,
                            ));
                      })))),
    );
  }

  Future<TouchPoints?> convert(TouchPoints? thing) async {
    await Future.delayed(const Duration(milliseconds: 25));
    // changing the delay for function does affect the
    // accuracy of painter results.
    // set this to 5 milliseconds and then there will be a smooth line, which is not required here.
    // set this to 25 milliseconds, or more, and then there will be a dotted line of x = y.

    return thing;
  }

  Stream<TouchPoints?> doStuff(List<TouchPoints?> things) {
    return Stream.fromIterable(things).asyncMap(convert);
    // This asyncMap seems to be the culprit here, My Guess!...
  }
}

class SignaturePainter extends CustomPainter {
  List<TouchPoints?> points;

  SignaturePainter({required this.points});
  @override
  void paint(Canvas canvas, Size size) {
    for (int i = 0; i < points.length - 1; i++) {
      if (shouldDrawLine(i)) {
        canvas.drawLine(
            points[i]!.points!, points[i + 1]!.points!, points[i]!.paint!);
      } else if (shouldDrawPoint(i)) {
        canvas.drawCircle(points[i]!.points!, points[i]!.paint!.strokeWidth / 2,
            points[i]!.paint!);
      }
    }
  }

  bool shouldDrawPoint(int i) =>
      points[i]?.points != null &&
      points.length > i + 1 &&
      points[i + 1]?.points == null;

  bool shouldDrawLine(int i) =>
      points[i]?.points != null &&
      points.length > i + 1 &&
      points[i + 1]?.points != null;

  @override
  bool shouldRepaint(SignaturePainter oldDelegate) {
    return true;
  }
}

class TouchPoints {
  Paint? paint;
  Offset? points;
  String? command;

  TouchPoints({this.points, this.paint, this.command});
}

```