首页 > 解决方案 > Variable declaration in Dart

问题描述

I'm just beginning to learn Dart and Flutter and I was wondering if there is any difference in the following declarations?

final List<WordPair> _suggestions = <WordPair>[];

and

final _suggestions = <WordPair>[];

They both seem to exhibit the same behaviour but I'm wondering if there is some underlying difference?

I prefer the first declaration as I'm coming from a C/C++ back ground

标签: dartflutter

解决方案


他们之间根本没有区别。第二种语法在这里只是为了避免无意义的重复。

通常你应该更喜欢 Dart 中的简写。 根据 dart 的 DO/DON'T,在某些情况下您可能希望使用完整的语法。

 final List<Foo> globalVariable = <Foo>[];


 void func() {
   final localVariable = <Foo>[]
 }

推荐阅读