11 个 Flutter 最佳实际
学习最佳实际,用 Flutter 进步代码品质、可读性、可维护性和生产率。
1. 将代码重构为 widgets 而不是 methods
重形成一个办法可能看起来很迷人,然而当构建办法太大时,它可能会从新构建,即便构建办法外部没有任何更改。然而,当波及到重构到一个 widgets 时,咱们失去了 widgets 生命周期的所有益处,因而只有当 widgets 中的某些内容发生变化时,它才会从新构建。因而,这能够避免不必要的从新构建,从而进步性能。这也提供了 Flutter 为 widgets 类提供的所有优化。
Column( children: [ Container( decoration: const BoxDecoration( color: Colors.red, ), child: const Text( 'Refactor to Widget', style: TextStyle( color: Colors.white, ), ), ), ], )
//Doclass RefactorWidget extends StatelessWidget { const RefactorWidget({ Key? key, }) : super(key: key); @override Widget build(BuildContext context) { return Container( decoration: const BoxDecoration( color: Colors.red, ), child: const Text( 'Refactor to Widget', style: TextStyle( color: Colors.white, ), ), ); }}//Do notContainer buildRefactorWidget() => Container( decoration: const BoxDecoration( color: Colors.red, ), child: const Text( 'Refactor to Widget', style: TextStyle( color: Colors.white, ), ),);
2. 尽可能应用 const 关键字
当咱们应用 setState()
Flutter 调用 build 办法并从新构建其中的每个 widgets 树。防止这种状况的最佳办法是应用常量构造函数。
在构建本人的 widgets 或应用 Flutter widgets 时,尽可能应用 const 构造函数。这有助于 Flutter 只从新构建应该更新的 widgets。
3. 应用级联运算符
如果咱们应该对同一个对象执行一系列操作,那么咱们应该抉择级联 ..
接线员。
//Dovar path = Path()..lineTo(0, size.height)..lineTo(size.width, size.height)..lineTo(size.width, 0)..close();//Do notvar path = Path();path.lineTo(0, size.height);path.lineTo(size.width, size.height);path.lineTo(size.width, 0);path.close();
4. 应用 spread collections
当现有我的项目曾经存储在另一个汇合中时,能够应用扩大汇合,因为扩大汇合语法使代码更简略、更容易。
//Dovar y = [4,5,6];var x = [1,2,...y];//Do notvar y = [4,5,6];var x = [1,2];x.addAll(y);
5. 应用 Null safe (??) 和 Null aware (?.) 操作符
在条件表达式中始终应用 ??
(如果为空)和 ?.
(为空)运算符而不是空查看。
//Doz = x ?? y;//Do notz = x == null ? y : x;
6. 防止应用 “as” 操作符代替 “is” 操作符
通常,如果无奈进行强制转换,则 as 强制转换操作符将引发异样。若要避免引发异样,能够应用。
//Doif (item is User)item.name = 'Amir';//Do not(item as User).name = 'Amir';
7. 不要显式初始化变量 null
在 Dart 中,当没有指定变量的值时,该变量直观地初始化为 null
,因而增加 null 是多余的,不须要。
//Doint counter;//Do notint counter = null;
8. 在 Flutter 应用 SizedBox 代替 Container
Container 是一个十分棒的 widgets,您将在 Flutter 宽泛应用它。 Container()
扩大到适宜父节点给出的束缚,并且不是常量构造函数。相同,SizedBox
是一个常量构造函数,构建一个固定大小的盒子。宽度和高度参数能够为 null
,以指定不应在相应的维度中束缚盒子的大小。因而,当咱们必须实现占位符时,应该应用 SizedBox
,而不是应用 Container
。
// Doreturn _isNotLoaded ? SizedBox.shrink() : YourWidget();//Do notreturn _isNotLoaded ? Container() : YourWidget();
9. 应用 raw 字符串
能够应用原始字符串来防止只本义反斜杠和美元。
//Dovar s = r'This is example of raw string \ and $';//Do notvar s = 'This is example of raw string \\ and \$';
10. 变量命名准则
Dart 有三种变数命名准则。
- UpperCamelCase 大骆驼峰
- lowerCamelCase 小骆驼峰
- lowercase_with_underscores 下划线连词
UpperCamelCase 大骆驼峰
// 类名Classes ( eg:- MyClass )class MyClass { … }// 枚举enum ( eg:- Status )enum Status { none, running, stopped, paused }// 类型定义typedefs ( eg:- Signature )typedef Signature<T> = bool Function(T value);// 泛型名type parameters ( eg:- T )class Foo<T extends Object> { … }// 扩大extension ( eg:- MyMethod )extension MyMethod<T> on List<T> { … }
lowercase_with_underscores 下划线连词
//packages 包 eg:- hive_flutter, url_launcher, flutter_svg//directories (mostly lowercase only) 目录 eg:- lib, bin, src, test, example//source files 文件 eg:- main.dart, home.dart//import prefixes 导包 eg:- import 'dart:math' as math; eg:- import 'dart:math' as my_math;
lowerCamelCase 小骆驼峰
// 外部类 变量// Class members (instance/static methods and variables) eg:- void myFun() {}, int studentRank = 1;// top-level definitions eg:- double topVariable;// variables eg:- int myValue = 3;// parameters (both positional and named) eg:- void fun({int myParam}){…} , void fun(int myParam) {…}
11. 应用 if 代替三元运算符语法, 在如下状况
当设计一个 Flutter 应用程序时,通常状况下须要有条件地出现不同的 widgets。您可能须要基于该平台生成一个 widgets,例如:
Row( children: [ Text("Amir"), Platform.isAndroid ? Text("This is From Android") : SizeBox(), Platform.isIOS ? Text("This is From IOS") : SizeBox(), ]);
在这种状况下,您能够删除三元运算符,并利用 Dart 的内置语法在数组中增加 if 语句。
Row( children: [ Text("Amir"), if (Platform.isAndroid) Text("This is From Android"), if (Platform.isIOS) Text("This if From IOS"), ]);
© 猫哥
- wx:ducafecat
- ducafecat.tech
本文由mdnice多平台公布