[flutter_markdown] fixes null check operator used on null value if onSelectionChanged is… (#6883)

- Fixes flutter/flutter/issues/149817
- Fixes flutter/flutter/issues/146282
diff --git a/packages/flutter_markdown/CHANGELOG.md b/packages/flutter_markdown/CHANGELOG.md
index c7e2912..f787fab 100644
--- a/packages/flutter_markdown/CHANGELOG.md
+++ b/packages/flutter_markdown/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.7.2+1
+
+* Fixes a crash caused by text selection when `selectable` is true and `onSelectionChanged` is null.
+
 ## 0.7.2
 
 * Multiple code blocks within a single markdown will now use separate ScrollControllers.
@@ -160,198 +164,198 @@
 
 ## 0.6.10
 
- * Update `markdown` dependency
+* Update `markdown` dependency
 
 ## 0.6.9+1
 
- * Remove build status badge from `README.md`
+* Remove build status badge from `README.md`
 
 ## 0.6.9
 
-  * Leading spaces in a paragraph and in list items are now ignored according to [GFM #192](https://github.github.com/gfm/#example-192) and [GFM #236](https://github.github.com/gfm/#example-236).
+* Leading spaces in a paragraph and in list items are now ignored according to [GFM #192](https://github.github.com/gfm/#example-192) and [GFM #236](https://github.github.com/gfm/#example-236).
 
 ## 0.6.8
 
-  * Added option paddingBuilders
+* Added option paddingBuilders
 
 ## 0.6.7
 
- * Fix `unnecessary_import` lint errors.
- * Added option pPadding
- * Added options h1Padding - h6Padding
+* Fix `unnecessary_import` lint errors.
+* Added option pPadding
+* Added options h1Padding - h6Padding
 
 ## 0.6.6
 
- * Soft line break
+* Soft line break
 
 ## 0.6.5
 
- * Fix unique Keys for RichText blocks
+* Fix unique Keys for RichText blocks
 
 ## 0.6.4
 
- * Fix merging of spans when first span is not a TextSpan
+* Fix merging of spans when first span is not a TextSpan
 
 ## 0.6.3
 
- * Fixed `onTap`, now the changed hyperlinks are reflected even with keeping the same link name unchanged.
+* Fixed `onTap`, now the changed hyperlinks are reflected even with keeping the same link name unchanged.
 
 ## 0.6.2
 
- * Updated metadata for new source location
- * Style changes to conform to flutter/packages analyzer settings
+* Updated metadata for new source location
+* Style changes to conform to flutter/packages analyzer settings
 
- ## 0.6.1
+## 0.6.1
 
- * Added builder option bulletBuilder
+* Added builder option bulletBuilder
 
 ## 0.6.0
 
- * Null safety release
- * Added stylesheet option listBulletPadding
- * Fixed blockquote inline styling
- * Added onTapText handler for selectable text
+* Null safety release
+* Added stylesheet option listBulletPadding
+* Fixed blockquote inline styling
+* Added onTapText handler for selectable text
 
 ## 0.6.0-nullsafety.2
 
- * Dependencies updated for null safety
+* Dependencies updated for null safety
 
 ## 0.6.0-nullsafety.1
 
- * Fix null safety on web
- * Image test mocks fixed for null safety
+* Fix null safety on web
+* Image test mocks fixed for null safety
 
 ## 0.6.0-nullsafety.0
 
- * Initial null safety migration.
+* Initial null safety migration.
 
 ## 0.5.2
 
- * Added `MarkdownListItemCrossAxisAlignment` to allow for intrinsic height
+* Added `MarkdownListItemCrossAxisAlignment` to allow for intrinsic height
    measurements of lists.
 
 ## 0.5.1
 
- * Fix user defined builders
+* Fix user defined builders
 
 ## 0.5.0
 
- * BREAKING CHANGE: `MarkdownTapLinkCallback` now has three parameters, not one, exposing more
+* BREAKING CHANGE: `MarkdownTapLinkCallback` now has three parameters, not one, exposing more
    information about a tapped link.
-   * Note for upgraders, the old single parameter `href` is now the second parameter to match the specification.
- * Android example upgraded
- * Test coverage updated to match GitHub Flavoured Markdown and CommonMark
- * Handle links with empty descriptions
- * Handle empty rows in tables
+  * Note for upgraders, the old single parameter `href` is now the second parameter to match the specification.
+* Android example upgraded
+* Test coverage updated to match GitHub Flavoured Markdown and CommonMark
+* Handle links with empty descriptions
+* Handle empty rows in tables
 
 ## 0.4.4
 
- * Fix handling of newline character in blockquote
- * Add new example demo
- * Use the start attribute in ordered list to set the first number
- * Revert changes made in PR #235 (which broke newline handling)
+* Fix handling of newline character in blockquote
+* Add new example demo
+* Use the start attribute in ordered list to set the first number
+* Revert changes made in PR #235 (which broke newline handling)
 
 ## 0.4.3
 
- * Fix merging of `MarkdownStyleSheets`
- * Fix `MarkdownStyleSheet` textScaleFactor to use default value of 1.0, if not provided, instead using the textScaleFactor of the nearest MediaQuery
+* Fix merging of `MarkdownStyleSheets`
+* Fix `MarkdownStyleSheet` textScaleFactor to use default value of 1.0, if not provided, instead using the textScaleFactor of the nearest MediaQuery
 
 ## 0.4.2
 
- * Fix parsing of image caption & alt attributes
- * Fix baseline alignment in lists
- * Support `LineBreakSyntax`
+* Fix parsing of image caption & alt attributes
+* Fix baseline alignment in lists
+* Support `LineBreakSyntax`
 
 ## 0.4.1
 
- * Downgrade Flutter minimum from 1.17.1 to 1.17.0 for Pub
+* Downgrade Flutter minimum from 1.17.1 to 1.17.0 for Pub
 
 ## 0.4.0
 
- * Updated for Flutter 1.17
- * Ignore newlines in paragraphs
- * Improve handling of horizontal rules
+* Updated for Flutter 1.17
+* Ignore newlines in paragraphs
+* Improve handling of horizontal rules
 
 ## 0.3.5
 
- * Fix hardcoded colors and improve Darktheme
- * Fix text alignment when formatting is involved
+* Fix hardcoded colors and improve Darktheme
+* Fix text alignment when formatting is involved
 
 ## 0.3.4
 
- * Add support for text paragraphs and blockquotes.
+* Add support for text paragraphs and blockquotes.
 
 ## 0.3.3
 
- * Add the ability to control the scroll position of the `MarkdownWidget`.
+* Add the ability to control the scroll position of the `MarkdownWidget`.
 
 ## 0.3.2
 
- * Uplift `package:markdown` dependency version to enable deleting HTML unescape URI workaround
- * Explictly state that Flutter 1.10.7 is the minimum supported Flutter version in the library `pubspec.yaml`.
+* Uplift `package:markdown` dependency version to enable deleting HTML unescape URI workaround
+* Explictly state that Flutter 1.10.7 is the minimum supported Flutter version in the library `pubspec.yaml`.
 
 ## 0.3.1
 
- * Expose `tableColumnWidth`
- * Add `MarkdownStyleSheet.fromCupertinoTheme`
- * Fix `MarkdownStyleSheet.blockquote`
- * Flutter for web support
- * Add physic and shrinkWrap to Markdown widget
- * Add MarkdownBody.fitContent
- * Support select text to copy
- * Fix list bullet alignment
- * HTML unescape URIs (temporary workaround for [dart-lang/markdown #272](https://github.com/dart-lang/markdown/issues/272))
- * Rebuilt `example/android` and `example/ios` directories
+* Expose `tableColumnWidth`
+* Add `MarkdownStyleSheet.fromCupertinoTheme`
+* Fix `MarkdownStyleSheet.blockquote`
+* Flutter for web support
+* Add physic and shrinkWrap to Markdown widget
+* Add MarkdownBody.fitContent
+* Support select text to copy
+* Fix list bullet alignment
+* HTML unescape URIs (temporary workaround for [dart-lang/markdown #272](https://github.com/dart-lang/markdown/issues/272))
+* Rebuilt `example/android` and `example/ios` directories
 
 **Note:** this version has an implicit minimum supported version of Flutter 1.10.7.
 See [flutter/flutter_markdown issue #156](https://github.com/flutter/flutter_markdown/issues/156) for more detail.
 
 ## 0.3.0
 
- * Support GitHub flavoured Markdown
- * Support strikethrough
- * Convert TextSpan to use new InlineSpan API
+* Support GitHub flavoured Markdown
+* Support strikethrough
+* Convert TextSpan to use new InlineSpan API
 
 ## 0.2.0
 
- * Updated environment sdk constraints to make the package
+* Updated environment sdk constraints to make the package
    Dart 2 compatible.  As a result, usage of this version and higher
    requires a Dart 2 SDK.
 
 ## 0.1.6
 
- * Updated `markdown` dependency.
+* Updated `markdown` dependency.
 
 ## 0.1.5
 
- * Add `mockito` as a dev dependency. Eliminate use of `package:http`, which
+* Add `mockito` as a dev dependency. Eliminate use of `package:http`, which
    is no longer part of Flutter.
 
 ## 0.1.4
 
- * Add `li` style to bullets
+* Add `li` style to bullets
 
 ## 0.1.3
 
- * Add `path` and `http` as declared dependencies in `pubspec.yaml`
+* Add `path` and `http` as declared dependencies in `pubspec.yaml`
 
 ## 0.1.2
 
- * Add support for horizontal rules.
- * Fix the `onTap` callback on images nested in hyperlinks
+* Add support for horizontal rules.
+* Fix the `onTap` callback on images nested in hyperlinks
 
 ## 0.1.1
 
- * Add support for local file paths in image links. Make sure to set the
+* Add support for local file paths in image links. Make sure to set the
    `imageDirectory` property to specify the base directory containing the image
    files.
 
 ## 0.1.0
 
- * Roll the dependency on `markdown` to 1.0.0
- * Add a test and example for image links
- * Fix the `onTap` callback on hyperlinks
+* Roll the dependency on `markdown` to 1.0.0
+* Add a test and example for image links
+* Fix the `onTap` callback on hyperlinks
 
 ## 0.0.9
 
- * First published version
+* First published version
diff --git a/packages/flutter_markdown/lib/src/builder.dart b/packages/flutter_markdown/lib/src/builder.dart
index fff7f39..8e7c98a 100644
--- a/packages/flutter_markdown/lib/src/builder.dart
+++ b/packages/flutter_markdown/lib/src/builder.dart
@@ -961,9 +961,10 @@
         text!,
         textScaler: styleSheet.textScaler,
         textAlign: textAlign ?? TextAlign.start,
-        onSelectionChanged:
-            (TextSelection selection, SelectionChangedCause? cause) =>
-                onSelectionChanged!(text.text, selection, cause),
+        onSelectionChanged: onSelectionChanged != null
+            ? (TextSelection selection, SelectionChangedCause? cause) =>
+                onSelectionChanged!(text.text, selection, cause)
+            : null,
         onTap: onTapText,
         key: k,
       );
diff --git a/packages/flutter_markdown/pubspec.yaml b/packages/flutter_markdown/pubspec.yaml
index 0336c96..f7b58ed 100644
--- a/packages/flutter_markdown/pubspec.yaml
+++ b/packages/flutter_markdown/pubspec.yaml
@@ -4,7 +4,7 @@
   formatted with simple Markdown tags.
 repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
 issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
-version: 0.7.2
+version: 0.7.2+1
 
 environment:
   sdk: ^3.3.0
diff --git a/packages/flutter_markdown/test/text_test.dart b/packages/flutter_markdown/test/text_test.dart
index 27f16cc..e8e8835 100644
--- a/packages/flutter_markdown/test/text_test.dart
+++ b/packages/flutter_markdown/test/text_test.dart
@@ -285,6 +285,50 @@
     );
 
     testWidgets(
+      'Selectable without onSelectionChanged',
+      (WidgetTester tester) async {
+        const String data = '# abc def ghi\njkl opq';
+
+        await tester.pumpWidget(
+          const MaterialApp(
+            home: Material(
+              child: MarkdownBody(
+                data: data,
+                selectable: true,
+              ),
+            ),
+          ),
+        );
+
+        // Find the positions before character 'd' and 'f'.
+        final Offset dPos = positionInRenderedText(tester, 'abc def ghi', 4);
+        final Offset fPos = positionInRenderedText(tester, 'abc def ghi', 6);
+        // Select from 'd' until 'f'.
+        final TestGesture firstGesture =
+            await tester.startGesture(dPos, kind: PointerDeviceKind.mouse);
+        addTearDown(firstGesture.removePointer);
+        await tester.pump();
+        await firstGesture.moveTo(fPos);
+        await firstGesture.up();
+        await tester.pump();
+
+        // Find the positions before character 'j' and 'o'.
+        final Offset jPos = positionInRenderedText(tester, 'jkl opq', 0);
+        final Offset oPos = positionInRenderedText(tester, 'jkl opq', 4);
+        // Select from 'j' until 'o'.
+        final TestGesture secondGesture =
+            await tester.startGesture(jPos, kind: PointerDeviceKind.mouse);
+        addTearDown(secondGesture.removePointer);
+        await tester.pump();
+        await secondGesture.moveTo(oPos);
+        await secondGesture.up();
+        await tester.pump();
+
+        expect(tester.takeException(), isNull);
+      },
+    );
+
+    testWidgets(
       'header with line of text and onSelectionChanged callback',
       (WidgetTester tester) async {
         const String data = '# abc def ghi\njkl opq';