sách gpt4 ai đã đi

Gọi bản dựng Flutter nhiều lần

In lại 作者:行者123 更新时间:2023-12-03 13:28:55 27 4
mua khóa gpt4 Nike

我有一个底部导航栏,它有一个列表页面,该页面使用状态块。

class _MainPageState extends State {
int _index = 0;
@ghi đè
Xây dựng tiện ích (BuildContext context) {
final List _widgets = [
const ListPage(),
Scaffold(),
Scaffold(),

];

return Scaffold(
body: IndexedStack(
index: _index,
children: _widgets,
),
bottomNavigationBar: BottomNavigationBar(
...


class ListPage extends StatelessWidget {
const ListPage({Key key}) : super(key: key);

@ghi đè
Xây dựng tiện ích (BuildContext context) {
return BlocProvider(
create: (_) =>
getIt()..add(const ListEvent.load(limit: 10)),
child: SafeArea(
child: Scaffold(
appBar: AppBar(),
body: const List(),
),
),
);
}
}
问题是 xây dựng被调用 4 次。这会导致事件获取列表 4 次。
不知道在哪里添加事件以防止重新构建。
如果我在 statefull 小部件的 initState 中添加事件.在从小部件树中获取 bloc 时,Bloc 无法识别上下文中的 ListBloc beeing。

1 Câu trả lời

最好的方法是在您的主要路线(包含底部导航的路线)的 initState 中添加事件。如下面的代码所示。

class _MainPageState extends State {
int _index = 0;

@ghi đè
void initState() {
super.initState();
getIt().add(const ListEvent.load(limit: 10));
}

@ghi đè
Xây dựng tiện ích (BuildContext context) {
final List _widgets = [
const ListPage(),
Scaffold(),
Scaffold(),

];

return Scaffold(
body: IndexedStack(
index: _index,
children: _widgets,
),
bottomNavigationBar: BottomNavigationBar(
...


class ListPage extends StatelessWidget {
const ListPage({Key key}) : super(key: key);

@ghi đè
Xây dựng tiện ích (BuildContext context) {
return BlocProvider(
create: (_) =>
getIt(),
child: SafeArea(
child: Scaffold(
appBar: AppBar(),
body: const List(),
),
),
);
}
}

关于多次调用 Flutter 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64961138/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com