Ados

a fullstack game worker

0%

每天进步一点点023 - Flutter项目源码阅读笔记

Preface

最近打算开发一些app,做长期的打算。
但是做某些方面还是有些不解,所以看一下别人开源的比较成熟的代码来学习。

Content

runZonedGuarded

来自dart:async

1
2
3
4
5
6
7
8
9
10
@Since("2.8")
R? runZonedGuarded<R>(
R body(),
void onError(
Object error,
StackTrace stack
),
{Map<Object?, Object?>? zoneValues,
ZoneSpecification? zoneSpecification}
)

在body本身的Zone范围内运行。

参考

sentry_flutter

flutter哨兵。用于激活自动报告错误,消息以及异常。

主要功能

  • 自动追踪本地崩溃错误
  • 离线存储事件
  • 利用设备数据
  • 等等

使用

  • 注册Sentry.io账号,然后在 http://sentry.io 上获取一个DNS
  • 安装此插件
  • 利用上面获得的DNS初始化Sentry SDK
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    // 普通初始化
    Future<void> main() async {
    await SentryFlutter.init(
    (options) {
    options.dsn = 'https://example@sentry.io/add-your-dsn-here';
    },
    // Init your App.
    appRunner: () => runApp(MyApp()),
    );
    }
    // 在自己的错误空间内初始化
    Future<void> main() async {
    runZonedGuarded(() async {
    await SentryFlutter.init(
    (options) {
    options.dsn = 'https://example@sentry.io/add-your-dsn-here';
    },
    );

    runApp(MyApp());
    }, (exception, stackTrace) async {
    await Sentry.captureException(exception, stackTrace: stackTrace);
    });
    }
    还有更多的功能,需要详细阅读插件相关api等。

参考

get.dart

介绍

融合了高性能的状态管理,智能依赖注入,快速实用的路由管理。

使用

1.用GetMaterialApp代替MaterialApp

1
void main() => runApp(GetMaterialApp(home: Home()

2. 制作业务逻辑类,使用 【.obs】 将变量变为可观察的

1
2
3
4
class Controller extends GetxController{
var count = 0.obs;
increment() => count++;
}

3. 使用StatelessWidget制作自定义视图可以节约一些内存

有了Get之后,就不需要StatefulWidget了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Home extends StatelessWidget {

@override
Widget build(context) {

// 通过 Get.put() 实例化你的逻辑类,这样他在所有的儿子节点中都可用
final Controller c = Get.put(Controller());

return Scaffold(
// 通过 Obx(()=> 在每次count更新的时候刷新 Text()
appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))),

// 用一句简单的 Get.to() 替换8 行Navigator.push
body: Center(child: ElevatedButton(
child: Text("Go to Other"), onPressed: () => Get.to(Other()))),
floatingActionButton:
FloatingActionButton(child: Icon(Icons.add), onPressed: c.increment));
}
}

class Other extends StatelessWidget {
// 在子节点中通过 Get 找到 Controller 实例
final Controller c = Get.find();

@override
Widget build(context){
// 访问到他的变量
return Scaffold(body: Center(child: Text("${c.count}")));
}
}

国际化

继承Translations,添加字典:

1
2
3
4
5
6
7
8
9
10
11
12
13
import 'package:get/get.dart';

class Messages extends Translations {
@override
Map<String, Map<String, String>> get keys => {
'en_US': {
'hello': 'Hello World',
},
'de_DE': {
'hello': 'Hallo Welt',
}
};
}

使用也很简单:

1
2
//加上.tr后缀就可以了
Text('title'.tr);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//区域定位
return GetMaterialApp(
translations: Messages(), // your translations
locale: Locale('en', 'US'), // translations will be displayed in that locale
fallbackLocale: Locale('en', 'UK'), // specify the fallback locale in case an invalid locale is selected.
);

// 更换区域
var locale = Locale('en', 'US');
Get.updateLocale(locale);

// 系统区域
return GetMaterialApp(
locale: Get.deviceLocale,
);

依赖注入主要是通过 Get.PutGet.lazyPut 实现。
其实这个所谓的依赖就是自定义的业务逻辑类。
还有更多封装好的功能,非常强大,需要区阅读文档。

参考