Flutter难点解析

1.跨页面更新数据

在使用GetX的时候,有时候需要跨页面修改数据,比如有ABC三个页面,在C页面的某个值更改值时,需要在A页面体现出来,这时候就需要用到Get.find<SomeController>();

但是如果SomeController没有注册,也就是没有调用Get.put()方法,就会发生Crash。这时候,我们需要判断SomeController是否注册过:

1
2
3
4
5
bool isControllerRegistered = GetInstance().isRegistered<SomeController>();
if (isControllerRegistered) {
SomeController controller = Get.find<SomeController>();
...
}

2.onInit不调用的问题

使用GetX的时候,有时候controlleronInit方法不调用,原因很可能是在view中没有调用controller的方法,controller没有加载的缘故

3.TabBar的问题

如果使用PageView配合BottomNavigationBarItem,在进入App的时候,所有Page都会加载,有些时候这样做会产生问题。(比如b需要a的一些数据,但是b已经加载完成了,a的数据还没请求成功)。
所有最好是使用下列示例代码(不使用Pageview):

1
2
3
4
5
6
7
8
9
10
11
Scaffold(
appBar: appbar,
body: pages[currentIndex],
bottomNavigationBar: BottomNavigationBar(
items: [
item1,
item2,
...
]
)
);

4.Text组件显示红色字体,带有黄色下划线

这是因为该Widget没有被ScaffoldDialog等包裹,因为这些组件内置DefaultTextStyle,如果你不想使用上述组件,也可以手动使用DefaultTextStyle包裹,这样Text就显示正常了。


Flutter难点解析
http://example.com/2023/11/02/Flutter难点解析/
Author
John Doe
Posted on
November 2, 2023
Licensed under