Skip to content

Instantly share code, notes, and snippets.

@anisalibegic
Created May 21, 2021 07:50
Show Gist options
  • Save anisalibegic/06ba5db014dfded055a1fde7d13fb9cf to your computer and use it in GitHub Desktop.
Save anisalibegic/06ba5db014dfded055a1fde7d13fb9cf to your computer and use it in GitHub Desktop.
This mixin overrides default setState method and adds a check if widget is still mounted in the widget tree.
import 'package:flutter/widgets.dart';
mixin SafeSetStateMixin<T extends StatefulWidget> on State<T> {
@override
void setState(VoidCallback fn) {
if (!mounted) return;
super.setState(fn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment