Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reordering when widget is rebuilding crashes #155

Open
sunilguptasg opened this issue May 14, 2022 · 2 comments
Open

Reordering when widget is rebuilding crashes #155

sunilguptasg opened this issue May 14, 2022 · 2 comments

Comments

@sunilguptasg
Copy link

I'm using a ReorderableColumn. One of the widgets is a file upload component. On file save, the widget displays a circular progress indicator. If I drag the widget with the animation active, there are several errors. This is due to the widget being unmounted during drag.

Error: setState() called after dispose(): _FilePickerWidgetState#174b4(lifecycle state: defunct, not mounted)

Is it possible that the widget stays mounted, and there is only a visual animation of drag? Once the index's are available of the drag position we can swap the widgets. I also have move up/move down buttons, and these do not cause any issues.

@sunilguptasg
Copy link
Author

The Flutter build-in ReorderableListView works - there are less controls available, but I no longer get any errors.

I'd still like to use Reorderables though if this issue can be fixed.

@nikiforosper
Copy link

nikiforosper commented Jan 18, 2024

same issue here.

Flow:

  1. Pick images
  2. Display the selected images on ReorderableWrap
  3. Make an http request when image selected (on ImagePickerItem initState)
  4. Reorder the Wrap

Issue
The issue is on step 4. While reordering, the ImagePickerItem rebuilding, calling again the initState

ReorderableWrap(
  spacing: 8.0,
  runSpacing: 4.0,
  alignment: WrapAlignment.center,
  padding: const EdgeInsets.all(8),
  needsLongPressDraggable: false,
  children: _selectedImages.map(
    (imageFile) {
      return ImagePickerItem(
        key: ValueKey(imageFile.path),
        image: imageFile,
      );
    },
  ).toList(),
  onReorder: (oldIndex, newIndex) {
    final File item = _selectedImages.removeAt(oldIndex);
    _selectedImages.insert(newIndex, item);
    if (!mounted) return;
    setState(() {});
  },
),
class ImagePickerItem extends StatefulWidget {
  const ImagePickerItem({
    super.key,
    required this.image,
  });
  final File image;

  @override
  State<ImagePickerItem> createState() => _ImagePickerItemState();
}

class _ImagePickerItemState extends State<ImagePickerItem> {
  bool isLoading = true;
  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        Card(
          child: Padding(
            padding: const EdgeInsets.all(4.0),
            child:
               Image.file(
                    widget.image,
                    height: 90,
                ),
          ),
        ),
        if (isLoading)
          const Positioned.fill(
            child: Center(
              child: CircularProgressIndicator.adaptive(),
            ),
          ),
      ],
    );
  }

  init() async {   
    await Future.delayed(const Duration(seconds: 1));
    if (mounted) {
      setState(() {
        isLoading = false;
      });
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants