Skip to content

Null data in Draggable throws error when dragging on top of DragTarget #131542

@chinmoy12c

Description

@chinmoy12c

Is there an existing issue for this?

Steps to reproduce

Currently, the data parameter in Draggable is nullable in nature. But this leads to Unexpected null value. error when a Draggable is dragged on top of a DragTarget when data == null since onAccept and onAcceptWithDetails are called as:

widget.onAccept?.call(avatar.data! as T);

widget.onAcceptWithDetails?.call(DragTargetDetails<T>(data: avatar.data! as T, offset: avatar._lastOffset!));

This also contradicts with onWillAccept since this allows data to be nullable while calling:

if (widget.onWillAccept == null || widget.onWillAccept!(avatar.data as T?)) {
__

Expected results

The app should not throw an error even if data is null.

Actual results

The app throws Unexpected null value. error when Draggable is dragged on top of DragTarget.

Code sample

Code sample
import 'package:flutter/material.dart';

/// Flutter code sample for [Draggable].

void main() => runApp(const DraggableExampleApp());

class DraggableExampleApp extends StatelessWidget {
  const DraggableExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Draggable Sample')),
        body: const DraggableExample(),
      ),
    );
  }
}

class DraggableExample extends StatefulWidget {
  const DraggableExample({super.key});

  @override
  State<DraggableExample> createState() => _DraggableExampleState();
}

class _DraggableExampleState extends State<DraggableExample> {
  int acceptedData = 0;

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        Draggable<int>(
          // Uncomment to make it work.
          // data: 10,
          feedback: Container(
            color: Colors.deepOrange,
            height: 100,
            width: 100,
            child: const Icon(Icons.directions_run),
          ),
          childWhenDragging: Container(
            height: 100.0,
            width: 100.0,
            color: Colors.pinkAccent,
            child: const Center(
              child: Text('Child When Dragging'),
            ),
          ),
          child: Container(
            height: 100.0,
            width: 100.0,
            color: Colors.lightGreenAccent,
            child: const Center(
              child: Text('Draggable'),
            ),
          ),
        ),
        DragTarget<int>(
          builder: (
            BuildContext context,
            List<dynamic> accepted,
            List<dynamic> rejected,
          ) {
            return Container(
              height: 100.0,
              width: 100.0,
              color: Colors.cyan,
              child: Center(
                child: Text('Value is updated to: $acceptedData'),
              ),
            );
          },
          onAccept: (int? data) {
            setState(() {
              acceptedData += data ?? 1;
            });
          },
        ),
      ],
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

The app is stuck when the error occurs.
Screenshot 2023-07-29 at 1 47 58 PM

Logs

Logs
══╡ EXCEPTION CAUGHT BY GESTURE LIBRARY ╞═══════════════════════════════════════════════════════════
The following TypeErrorImpl was thrown while routing a pointer event:
Unexpected null value.

When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:49      throw_
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 855:63  nullCheck
packages/flutter/src/widgets/drag_target.dart 720:38                              didDrop
packages/flutter/src/widgets/drag_target.dart 885:7                               finishDrag
packages/flutter/src/widgets/drag_target.dart 798:5                               end
packages/flutter/src/gestures/multidrag.dart 156:13                               [_up]
packages/flutter/src/gestures/multidrag.dart 258:12                               [_handleEvent]
packages/flutter/src/gestures/pointer_router.dart 98:12                           [_dispatch]
packages/flutter/src/gestures/pointer_router.dart 143:9                           <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/linked_hash_map.dart 21:13          forEach
packages/flutter/src/gestures/pointer_router.dart 141:17                          [_dispatchEventToRoutes]
packages/flutter/src/gestures/pointer_router.dart 127:7                           route
packages/flutter/src/gestures/binding.dart 488:19                                 handleEvent
packages/flutter/src/gestures/binding.dart 468:14                                 dispatchEvent
packages/flutter/src/rendering/binding.dart 439:11                                dispatchEvent
packages/flutter/src/gestures/binding.dart 413:7                                  [_handlePointerEventImmediately]
packages/flutter/src/gestures/binding.dart 376:5                                  handlePointerEvent
packages/flutter/src/gestures/binding.dart 323:7                                  [_flushPointerEventQueue]
packages/flutter/src/gestures/binding.dart 292:9                                  [_handlePointerDataPacket]
lib/_engine/engine/platform_dispatcher.dart 1319:13                               invoke1
lib/_engine/engine/platform_dispatcher.dart 303:5                                 invokeOnPointerDataPacket
lib/_engine/engine/pointer_binding.dart 168:39                                    [_onPointerData]
lib/_engine/engine/pointer_binding.dart 791:20                                    <fn>
lib/_engine/engine/pointer_binding.dart 720:14                                    <fn>
lib/_engine/engine/pointer_binding.dart 317:16                                    loggedHandler
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 574:37  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 579:39  dcall

router: Instance of 'PointerRouter'
route: Closure: (PointerEvent) => void from: function () { [native code] }
event: PointerUpEvent#64271(position: Offset(836.3, 154.2))
════════════════════════════════════════════════════════════════════════════════════════════════════

Flutter Doctor output

Doctor output
[✓] Flutter (Channel master, 3.13.0-12.0.pre.31, on macOS 13.4.1 22F82 darwin-arm64, locale en-IN)
    • Flutter version 3.13.0-12.0.pre.31 on channel master at /Users/chinmoy/MasterFiles/opensource/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e94e185c36 (47 minutes ago), 2023-07-29 03:33:30 -0400
    • Engine revision 24ccd8fffb
    • Dart version 3.2.0 (build 3.2.0-15.0.dev)
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/chinmoy/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/chinmoy/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] IntelliJ IDEA Community Edition (version 2023.1.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.79.2)
    • VS Code at /Users/chinmoy/Downloads/Visual Studio Code.app/Contents
    • Flutter extension version 3.68.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.4.1 22F82 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 114.0.5735.248

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: duplicateIssue is closed as a duplicate of an existing issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions