[英]How does flutter_riverpod in Flutter convert ConsumerWidget to ConsumerStatefulWidget more conveniently?
I am using flutter_riverpod
package.我正在使用flutter_riverpod
包。
I want to convert ConsumerWidget
to ConsumerStatefulWidget
.我想将ConsumerWidget
转换为ConsumerStatefulWidget
。
However, it took me a lot of time to do it like this:但是,我花了很多时间才这样做:
class Widget extends ConsumerWidget {
const Widget({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return …;
}
}
ref
) (删除ref
)class Widget extends ConsumerWidget {
const Widget({super.key});
@override
Widget build(BuildContext context) {
return …;
}
}
ConsumerWidget
to StatelessWidget
) (将ConsumerWidget
更改为StatelessWidget
)class Widget extends StatelessWidget {
const Widget({super.key});
@override
Widget build(BuildContext context) {
return …;
}
}
Convert to StatefulWidget
action) (由Convert to StatefulWidget
操作转换)class Widget extends StatefulWidget {
const Widget({super.key});
@override
State<Widget> createState() => _WidgetState();
}
class _WidgetState extends State<Widget> {
@override
Widget build(BuildContext context) {
return …;
}
}
Consumer
word) (加Consumer
词)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.如您所见,我需要使用 5 个步骤来完成。 Is there a faster way?有没有更快的方法?
Feel free to leave a comment if you need more information.如果您需要更多信息,请随时发表评论。
How to convert ConsumerWidget
to ConsumerStatefulWidget
more easily?如何更简单地将ConsumerWidget
转换为ConsumerStatefulWidget
? I would appreciate any help.我将不胜感激任何帮助。 Thank you in advance!先感谢您!
If my ConsumerWidget
has no parameters,如果我的ConsumerWidget
没有参数,
ConsumerStatefulWidget
with the same name. 1.新建一个同名的ConsumerStatefulWidget
。build
method of ConsumerWidget
to a new created widget.将ConsumerWidget
的build
方法的内容复制到新创建的小部件。ConsumerWidget
) Copy content of ConsumerWidget
to a new created widget. (如果ConsumerWidget
中还有其他方法)将ConsumerWidget
的内容复制到新创建的小部件中。 Then remove ref
from build
.然后从build
中删除ref
。ConsumerWidget
. 3.删除ConsumerWidget
。 If my ConsumerWidget
has parameters, then I'm doing the same as you, because otherwise I'll have to add a widget.
如果我的ConsumerWidget
有参数,那么我会和你一样做,否则我将不得不添加一个widget.
before widget variables.在小部件变量之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.