I think the most important thing working with flutter and having a nice development experience is to have a cristal clear structure. 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. 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.
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;
});
}
,上面的代码应适当地覆盖为可读的大声笑
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. So manageable nesting is allowed in most of the places.
Though I have seen there are some unnecessary nesting done on your provided example.
Here we could have avoided the above nesting using await keyword something like below
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
and so on. I'm quite willing to put a single widget or maybe two in a separate function and give it a meaningful name if it makes sense. Even though I'm working on a large monitor and I couldn't care less about harmful old habits like 80-character lines, I don't go deeper than three, maybe four levels at most without very strong reasons, and I don't recall having met any such reasons recently.
You don't always need indentation, either. For simple things like:
Expanded(child: buildList(context)),
I always keep them on one line. Also with short, simple widgets like:
Of course, with any more complicated and reused fragment, you probably want to put it into a custom widget on its own, anyway — just like this EmptyText above that's nothing really more than a Column with an icon and a centered text below. A simple stateless widget with a single build() function, that's all. But because I use it in several places and because it's much more convenient to refer to it with the single line above than to include the same Column again and again, it went into a widget of its own.
What I'm sure to avoid at all costs are nested ternary operators, especially when the branches are complex trees — I consider them the worst offenders against readability. I always lift the branches out into functions of their own and only refer to them:
return ifSomething ? buildOne() : buildAnother();
By the way, you can always resort to tricks like the nested package if you really need nesting but you don't want to go to the right.
It is an important part of the declarative way flutter works. 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
发布评论
评论(4)
我认为,与扑朔迷离和拥有良好的发展经验一起使用的最重要的事情就是拥有一个清晰的结构。您可以遵循很多结构,但是扑动的方式是很多嵌套。例如,在更大的项目中,我遵循这个概念:
我对此感到满意。我有用于API,模型,业务逻辑,页面的CLEA场所,并且由于您可以重构代码并从中创建小部件,因此我的页面比您在github示例中看到的小。我还可以快速找到小部件,这些小部件在我的应用程序中多个位置都使用。
找到一个良好的结构并创建许多小部件,以后很容易理解您的代码。我当然只能为自己说话,因为我很舒服。
I think the most important thing working with flutter and having a nice development experience is to have a cristal clear structure. 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:
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. 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.
重要的是要注意,无论是HTML,Android XML左右,我们都会筑巢。因此,大多数地方都允许筑巢。
尽管我已经看到您提供的示例有一些不必要的嵌套。
例如
在这里,我们可以使用
等待
关键字避免上述嵌套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. 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 e.g.
Here we could have avoided the above nesting using
await
keyword something like below当然。您只需提起这样的树的任何更深的部分,然后将其放入单独的功能中。我的代码通常看起来像这样:
然后:
等等。我很愿意将一个小部件或两个小部件放在单独的功能中,并在有意义的情况下给它一个有意义的名称。即使我正在使用一个大型监视器,但我也不关心80个字符等有害的旧习惯,但我的进度不超过三个,也许最多有四个级别,而没有非常有力的理由,我不知道t召回最近满足了任何此类原因。
您也不总是需要凹痕。对于简单的事情,例如:
我总是将它们保持在一行。另外,使用简短的小部件,例如:
当然,如果有任何复杂和重复使用的片段,您可能想单独将其放入自定义的小部件中 - 就像This
emptyText
一样不仅仅是带有图标的列
,下面的中心文本。一个带有单个build()
函数的简单无状态小部件,仅此而已。但是,因为我在多个位置和中使用它,因为它与上面的单行相比,它更方便,而不是一次又一次地包括相同的column
它自己的小部件。我一定要避免的是嵌套的三元操作员,尤其是当树枝是复杂的树木时 - 我认为它们是最糟糕的罪犯,反对可读性。我总是将分支提升到自己的功能中,只提到它们:
顺便说一句,您总是可以诉诸于嵌套软件包,如果您确实需要嵌套,但您不想向右走。
Sure. You simply lift out any deeper part of a tree like this and put it into a separate function. My code usually looks like this:
and then:
and so on. I'm quite willing to put a single widget or maybe two in a separate function and give it a meaningful name if it makes sense. Even though I'm working on a large monitor and I couldn't care less about harmful old habits like 80-character lines, I don't go deeper than three, maybe four levels at most without very strong reasons, and I don't recall having met any such reasons recently.
You don't always need indentation, either. For simple things like:
I always keep them on one line. Also with short, simple widgets like:
Of course, with any more complicated and reused fragment, you probably want to put it into a custom widget on its own, anyway — just like this
EmptyText
above that's nothing really more than aColumn
with an icon and a centered text below. A simple stateless widget with a singlebuild()
function, that's all. But because I use it in several places and because it's much more convenient to refer to it with the single line above than to include the sameColumn
again and again, it went into a widget of its own.What I'm sure to avoid at all costs are nested ternary operators, especially when the branches are complex trees — I consider them the worst offenders against readability. I always lift the branches out into functions of their own and only refer to them:
By the way, you can always resort to tricks like the nested package if you really need nesting but you don't want to go to the right.
这是声明性方式颤抖起作用的重要组成部分。所以不,恕我直言,您无法避免筑巢。如果可以的话,那将是一个解决方法,绕过了颤抖的基本概念。
就像我说的那样,只要使用颤抖的方式。
It is an important part of the declarative way flutter works. 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