Skip to content

Instantly share code, notes, and snippets.

@arxdeus
Created April 21, 2025 17:15
Show Gist options
  • Save arxdeus/585eb6dedc646ea46982b826b0456ab7 to your computer and use it in GitHub Desktop.
Save arxdeus/585eb6dedc646ea46982b826b0456ab7 to your computer and use it in GitHub Desktop.
unsafe_variance lint on `builder`
import 'package:flutter/widgets.dart';
class SomeBuilder<T> extends StatefulWidget {
const SomeBuilder({
required this.initialValue,
required this.builder,
this.child,
super.key, // ignore: unused_element
});
final T initialValue;
final ValueWidgetBuilder<T> builder; // LINT - This type is unsafe: a type parameter occurs in a non-covariant position
final Widget? child;
@override
State<SomeBuilder<T>> createState() => _SomeBuilderState<T>();
}
/// State for widget SomeBuilder.
class _SomeBuilderState<T> extends State<SomeBuilder<T>> {
late final ValueNotifier<T> _notifier = ValueNotifier(widget.initialValue);
@override
Widget build(BuildContext context) => ValueListenableBuilder(
valueListenable: _notifier,
builder: (context, value, child) => widget.builder(context, value, child),
child: widget.child,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment