简体   繁体   English

有没有办法避免 Flutter 小部件的嵌套?

[英]Is there a way to avoid a nesting of Flutter widgets?

I am going to try Flutter, but all examples on GitHub I've found includes code with a high nesting level of widgets that is bad for readability.我将尝试 Flutter,但我在 GitHub 上发现的所有示例都包含具有高嵌套级别的小部件的代码,这不利于可读性。 For example, this one: https://github.com/smartherd/Flutter-Demos/blob/master/lib/screens/note_list.dart比如这个: https ://github.com/smartherd/Flutter-Demos/blob/master/lib/screens/note_list.dart

Can I somehow avoid it?我能以某种方式避免它吗? Is there a better practice to improve my developer experience?有更好的做法来改善我的开发人员体验吗?

I think the most important thing working with flutter and having a nice development experience is to have a cristal clear structure.我认为使用 Flutter 并拥有良好的开发体验最重要的是拥有清晰的结构。 There are plenty structures you can follow, but the way flutter works is a lot of nesting.您可以遵循很多结构,但颤振的工作方式是大量嵌套。 Me for example in bigger projects I'm following this concept:例如,我在更大的项目中遵循这个概念:

- [folder] lib
    - [folder] models
    - [folder] apis
    - [folder] bloc (or any other state management / business logic pattern)
    - [folder] pages
        - [folder] page 1
        - [folder] page 2
        - [folder] page 3
            - pageview.dart
            - widget 1.dart
            - widget 2.dart
            - widget 3.dart
    - [folder] widgets (every widget which is used appwide)
    - main.dart

Im feeling comfortable with this.我对此感到很舒服。 I have clea places for api, models, business logic, pages and since you can refactor your code and create widgets out of it, my pages are smaller than what you are seeing on your github example.我为 api、模型、业务逻辑、页面提供了干净的位置,并且由于您可以重构代码并从中创建小部件,因此我的页面比您在 github 示例中看到的要小。 I can also find widgets fast which are used on more than one place in my app.我还可以快速找到在我的应用程序中不止一个地方使用的小部件。

Finding a good structure and create a lot widgets makes it easy to understand your code later on.找到一个好的结构并创建很多小部件可以让您以后更容易理解您的代码。 I can ofcourse only talk for myself, for me its very comfortable.我当然只能为自己说话,对我来说很舒服。

It is important to note that most of the places, we do some kind of nesting whether it is HTML, Android XML or so on.需要注意的是,在大多数地方,我们都会进行某种嵌套,无论是 HTML、Android XML 等。 So manageable nesting is allowed in most of the places.因此,大多数地方都允许进行可管理的嵌套。

Though I have seen there are some unnecessary nesting done on your provided example.尽管我已经看到在您提供的示例中做了一些不必要的嵌套。

For eg例如

 void updateListView() { final Future<Database> dbFuture = databaseHelper.initializeDatabase(); dbFuture.then((database) { Future<List<Note>> noteListFuture = databaseHelper.getNoteList(); noteListFuture.then((noteList) { setState(() { this.noteList = noteList; this.count = noteList.length; }); }); }); }

Here we could have avoided the above nesting using await keyword something like below在这里,我们可以使用await关键字来避免上面的嵌套,如下所示

 void updateListView() { // I don't know why is it even there but if it is required then it should be done in // below way which is commented out // final Database database = await databaseHelper.initializeDatabase(); List<Note> noteList = await databaseHelper.getNoteList(); setState(() { this.noteList = noteList; this.count = noteList.length; }); }

And the above code should be properly linted to be readable lol并且上面的代码应该被正确地整理成可读的大声笑

It is an important part of the declarative way flutter works.它是 Flutter 声明式工作方式的重要组成部分。 So no, IMHO, you cannot avoid nesting.所以不,恕我直言,你不能避免嵌套。 And if you could, it would be a workaround, circumventing the basic concepts of flutter.. Just use flutter the way it's intended.. As i said: just my humble opinion如果可以的话,这将是一种解决方法,绕过颤振的基本概念..只需按照预期的方式使用颤动..正如我所说:只是我的拙见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM