I am using flutter_riverpod
package.
I want to convert ConsumerWidget
to ConsumerStatefulWidget
.
However, it took me a lot of time to do it like this:
- (code)
class Widget extends ConsumerWidget {
const Widget({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return …;
}
}
- (remove
ref
)
class Widget extends ConsumerWidget {
const Widget({super.key});
@override
Widget build(BuildContext context) {
return …;
}
}
- (change
ConsumerWidget
toStatelessWidget
)
class Widget extends StatelessWidget {
const Widget({super.key});
@override
Widget build(BuildContext context) {
return …;
}
}
- (converted by the
Convert to StatefulWidget
action)
class Widget extends StatefulWidget {
const Widget({super.key});
@override
State<Widget> createState() => _WidgetState();
}
class _WidgetState extends State<Widget> {
@override
Widget build(BuildContext context) {
return …;
}
}
- (add
Consumer
word)
class Widget extends ConsumerStatefulWidget {
const Widget({super.key});
@override
ConsumerState<Widget> createState() => _WidgetState();
}
class _WidgetState extends ConsumerState<Widget> {
@override
Widget build(BuildContext context) {
return …;
}
}
As you can see, I need to use 5 steps to do it. Is there a faster way?
Feel free to leave a comment if you need more information.
How to convert ConsumerWidget
to ConsumerStatefulWidget
more easily? I would appreciate any help. Thank you in advance!
If my
ConsumerWidget
has no parameters,ConsumerStatefulWidget
with the same name.build
method ofConsumerWidget
to a new created widget.ConsumerWidget
) Copy content ofConsumerWidget
to a new created widget. Then removeref
frombuild
.ConsumerWidget
.If my
ConsumerWidget
has parameters, then I'm doing the same as you, because otherwise I'll have to add awidget.
before widget variables.