1. Waiting for another flutter command to release the startup lock…
1 | // 杀死dart进程 |
2 | flutter packages pub build_runner watch |
3 | |
4 | // 终极方案,删除flutter SDK文件夹目录下的bin/cache下边的lockfile文件 |
5 | rm ./flutter/bin/cache/lockfile |
2. Could not find a file named “pubspec.yaml” in https://github.com/MarkOSullivan94/dart_config.git 7a88fbc5fd987dce78e468ec45e9e841a49f422d.
1 | // 删除flutter SDK文件夹目录下的.pub-cache下边的git文件夹 |
2 | rm ./flutter/.pub-cache/git |
3.点击任意空白处,软键盘收起
1 | GestureDetector( |
2 | /// 透明也响应处理 |
3 | behavior: HitTestBehavior.translucent, |
4 | onTap: () { |
5 | /// 触摸收起键盘 |
6 | FocusScope.of(context).requestFocus(FocusNode()); |
7 | }, |
8 | child: Container( |
9 | /// xxx |
10 | ), |
11 | ); |
4. Oops; flutter has exited unexpectedly. Sending crash report to Google.
1 | /// 在终端执行命令 |
2 | xcode-select --install |
5. 页面返回隐藏虚拟导航条
1 |
|
2 | void deactivate() { |
3 | SystemChrome.restoreSystemUIOverlays(); |
4 | super.deactivate(); |
5 | } |
6. 如果没有网络连接,如何启动静态html页面而不是URL ?【webview】
1 | Future check() async { |
2 | try { |
3 | final result = await InternetAddress.lookup('google.com'); |
4 | if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) { |
5 | connectionStatus = true; |
6 | print("connected $connectionStatus"); |
7 | } |
8 | } on SocketException catch (_) { |
9 | connectionStatus = false; |
10 | print("not connected $connectionStatus"); |
11 | } |
12 | } |
13 | |
14 | FutureBuilder( |
15 | future: check(), |
16 | builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) { |
17 | if (connectionStatus == true) { |
18 | /// 如果网络正常连接 |
19 | return SafeArea( |
20 | child: WebviewScaffold( |
21 | url: "http://www.baidu.com", |
22 | ), |
23 | ), |
24 | } else { |
25 | /// 如果网络连接失败 |
26 | return SafeArea( |
27 | child: WebviewScaffold( |
28 | url: Uri.dataFromString('<html><body>hello world</body></html>', |
29 | mimeType: 'text/html').toString() |
30 | ), |
31 | ), |
32 | } |
33 | } |
34 | ) |
7. 判断当前路由是否在栈顶
1 | // ModalRoute.of(context) API可以获取当前路由对象以及当前页面的所有属性 |
2 | // 如果路由active,还位于最顶层,则isCurrent为true |
3 | ModalRoute.of(context).isCurrent |
8. B widget嵌套在A Widget里(两个Widget分别在不同的Class里),如何在B Widget里获取A Widget的数据(变量、state、方法等等)并修改呢?
1 | class A extends StatefulWidget { |
2 | A({Key key}) : super(key: key); |
3 | _AState createState() => _AState(); |
4 | } |
5 | |
6 | class _AState extends State<A> { |
7 | /// 这个是A widget定义的一个变量 |
8 | var name = 'Jerry'; |
9 | |
10 | /// 这个是B Widget里声明的方法 |
11 | void getName() { |
12 | /// TODO |
13 | } |
14 | |
15 | |
16 | Widget build(BuildContext context) { |
17 | return Container( |
18 | /// 这里嵌套了B Widget |
19 | child: B(), |
20 | ); |
21 | } |
22 | } |
23 | |
24 | class B extends StatefulWidget { |
25 | B({Key key}) : super(key: key); |
26 | _BState createState() => _BState(); |
27 | } |
28 | |
29 | class _BState extends State<B> { |
30 | |
31 | Widget build(BuildContext context) { |
32 | return Container( |
33 | child: InkWell( |
34 | child: Text('this is B Widget!'), |
35 | onTap: () { |
36 | setState(() { |
37 | /// 在B Widget里边怎么获取A Widget的变量并修改呢?, 方案如下: |
38 | _AState aWidgetState = context.ancestorStateOfType(TypeMatcher<_AState>()); |
39 | /// 获取A里的变量 |
40 | aWidgetState.name = 'Anna'; |
41 | /// 调用A里的方法 |
42 | aWidgetState.getName(); |
43 | }); |
44 | }, |
45 | ), |
46 | ); |
47 | } |
48 | } |
9. B widget嵌套在A Widget里(两个Widget分别在不同的Class里),如何在A Widget里获取B Widget里的数据(变量、state、方法等等)呢?
1 | class A extends StatefulWidget { |
2 | A({Key key}) : super(key: key); |
3 | _AState createState() => _AState(); |
4 | } |
5 | |
6 | class _AState extends State<A> { |
7 | final GlobalKey<_BState> _bStateKey = GlobalKey(); |
8 | |
9 | Widget build(BuildContext context) { |
10 | /// 获取B里边的变量 |
11 | print(_bStateKey.currentState.name); |
12 | /// 调用B里边的方法 |
13 | _bStateKey.currentState.getName(); |
14 | |
15 | return Container( |
16 | child: B(), |
17 | ); |
18 | } |
19 | } |
20 | |
21 | class B extends StatefulWidget { |
22 | B({Key key}) : super(key: key); |
23 | _BState createState() => _BState(); |
24 | } |
25 | |
26 | class _BState extends State<B> { |
27 | /// 这个是B Widget里声明的变量 |
28 | var name = 'Jerry'; |
29 | |
30 | /// 这个是B Widget里声明的方法 |
31 | void getName() { |
32 | /// TODO |
33 | } |
34 | |
35 | |
36 | Widget build(BuildContext context) { |
37 | return Container( |
38 | child: Text('This is B Widget!'), |
39 | ); |
40 | } |
41 | } |
10. Exception: ideviceinfo return an error: ERROR: Could not connect to lockdownd, error code -17(或者-21)
1 | // 以下两种方式可供使用: |
2 | |
3 | 1. 如果你的设备上连接有真机,用的模拟器测试,拔掉手机手机的USB线,再试试运行到模拟器。 |
4 | |
5 | 2. 断开设备,在终端窗口中,键入:sudo pkill usbmuxd(它将自动重新启动),再次连接设备 |