Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

Flutter - Is It OK to Put State in MyApp?

I was looking at Flutter's docs, and this diagram shows the cart model being put in MyApp. Now, in the flutter default project, MyApp is a StatelessWidget. Is that OK -- the idea being that you pass references to the model down via constructors -- or is it supposed to be an InheritedWidget? Or does it matter? Thanks for any insights ...

question from:https://stackoverflow.com/questions/65837264/flutter-is-it-ok-to-put-state-in-myapp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, of course you can make MyApp a stateful widget. However sometimes that would not be efficient. For example, say you have a int counter = 0; in the state of MyApp. Then when you change counter, you call setState(() {counter++;}); But then the whole MyApp will get rebuild! That takes some time if you app is complex.

Thus, I suggest some more automatic library, such as MobX (https://github.com/mobxjs/mobx.dart). Personally I used it to write a simple project about 30k lines of flutter code and it works pretty well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...