简体   繁体   English

你如何解决 Flutter/Dart 中的“右括号综合症”?

[英]How do you solve the "closing brackets syndrome" in Flutter/Dart?

I think, one of the most annoying problems when coding Flutter is when you become confused/out of sync of those closing brackets.我认为,编码 Flutter 时最烦人的问题之一是当您对这些右括号感到困惑/不同步时。

在此处输入图像描述

To me, it happens often that I add a new parent widget or insert a code snipped and then the closing curly/square/round brackets do not match, or semicolon/colon is missing... Of course, it is solvable: go to each bracket and the tooling will show you the counter-bracket and maybe you detect the discrepancy fast...or not.对我来说,经常发生我添加一个新的父小部件或插入一个代码剪断然后右花括号/方括号/圆括号不匹配,或者缺少分号/冒号......当然,这是可以解决的:go每个支架和工具都会向您显示反支架,也许您可以快速检测到差异......或者没有。 As you can see in the snipped, the tooling helps a bit by showing the corresponding class/widget.正如您在截图中看到的那样,该工具通过显示相应的类/小部件有所帮助。 But to get it into sync, it always takes ages....但要让它同步,总是需要很长时间......

Do you have any best practises or hints/suggestions how to quickly adjust the brackets?您是否有任何最佳实践或提示/建议如何快速调整括号? What is your experience?你的经验是什么?

Thanks!谢谢!

In VsCode you have some very usefull shortcuts to:在 VsCode 中,您有一些非常有用的快捷方式:

  • remove a widget删除一个小部件
  • surround an existing widget with a new one用新的小部件包围现有的小部件
  • and more和更多

And it handles the adding/removing of brackets:它处理括号的添加/删除:

在此处输入图像描述

Gif took from this answer Gif 取自这个答案

There are some suggestions for you有一些建议给你
1 - Switch to vscode its lighter, faster and you can use its intelli sense or what we call code action to add widgets and remove wherever you want in your flutter code. 1 - 切换到更轻、更快的 vscode,您可以使用它的智能感知或我们所说的代码操作来添加小部件并删除 flutter 代码中您想要的任何位置。 By doing this it will automatically take care of brackets and stuff.通过这样做,它会自动处理括号和东西。
2- Use flutter snippets extension there are a lot of them available on vscode at-least, not sure about android studio but the extension it will auto generate a boilerplate template for you along with the brackets 2- 使用 flutter 片段扩展,至少在 vscode 上有很多可用的片段,不确定 android 工作室,但扩展它将自动为您生成样板模板以及括号
3- You can go to vscode or your android studio's flutter extension settings and customize it, there are options for auto formatting and importing as well. 3-您可以 go 到 vscode 或您的 android 工作室的 flutter 扩展设置和自定义它,还有自动格式化和导入的选项。
4- (Optional) If you have github co-pilot then it auto suggests you about the brackets and code but not always accurate and also co-pilot is not officially available for all but its way too good to have. 4- (可选)如果您有 github 副驾驶,那么它会自动向您建议括号和代码,但并不总是准确的,而且副驾驶并非对所有人都正式可用,但它的方式太好了。

Quick Tip!小建议!

I use IDE integrated features to avoid mistakes and it saves my time as well.我使用IDE 集成功能来避免错误,它也节省了我的时间。 Check below example.检查下面的例子。 Don't try to wrap manually.不要尝试手动包装。

用容器、列、行或任何其他小部件包装

I found this article myself quite useful when it comes to avoid bracketing mistakes and fasten developments.我发现这篇文章在避免括号错误和加快开发方面非常有用。

You can seperate your code to functions or even better create your own Widgets.您可以将代码分离为函数,甚至更好地创建自己的小部件。 Let's让我们
say you have to display bunch of movies as gird of cards on a main page.假设您必须在主页上将一堆电影显示为卡片组。 Instead nesting them inside giant Scaffold you can create而是将它们嵌套在您可以创建的巨型脚手架内

  1. ListOfMovies widget using GridView which consumes List<Movie> ListOfMovies小部件使用GridView消耗List<Movie>
  2. CardMovie widget which consumes single Movie object Here is how I seperated them on my own project. CardMovie小部件消耗单个Movie object 这是我在自己的项目中分离它们的方法。 This is not the perfect but may be helpful这不是完美的,但可能会有所帮助在此处输入图像描述

Edit.编辑。 Here is another minor tip: On JetBrains IDEs you can jump to other end of brackets or curly brackets using Ctrl+Shift+M command这是另一个小提示:在 JetBrains IDE 上,您可以使用Ctrl+Shift+M命令跳转到括号或大括号的另一端

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

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