0
class Example extends StatelessWidget {
  final List<Widget> children;

  const Example({super.key, required this.children});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<ScrollController>(
      create: (_) => ScrollController(),
      child: ListView(
        controller: context.read<ScrollController>(),
        children: children,
      ),
    );
  }
}

Same with the TextEditingController.

This approach seems more concise to me, but some of my colleagues do not share my views.

Ринат
  • 96
  • 1
  • 2
  • Provider is also using Stateful widget under the hood but its better to use not StatefulWidget . Besides u don't need to use ChangeNotifierProvider, instead u can do this Provider.of(context, listen: true); this listen parameter will do the state changing work under the hood – JB Jason Feb 22 '23 at 16:13
  • Your answer is not very clear, I can’t replace ChangeNotifierProvider with Provider.of, because ChangeNotifierProvider creates an object and Provider.of... reads it or watches, these are absolutely opposite things, and I use ChangeNotifierProvider because ScrollController extends ChangeNotifier and Normal Provider does not fit here – Ринат Feb 22 '23 at 19:45
  • got it, Theb can u give any hints about what they r prefering instead urs – JB Jason Feb 23 '23 at 08:03
  • StatefulWidget with initState and dispose to create and dispose controller – Ринат Feb 23 '23 at 09:17

0 Answers0