English 中文(简体)
如果未来名单空出,如何改变禁忌
原标题:How to change tab on tabbar if Future List returns empty
  • 时间:2023-09-04 20:59:01
  •  标签:
  • flutter
  • dart

我有一个带2个表格(服务和教练)的塔巴。 我用2个未来清单(在表格上使用未来设计者)填写这一表格。 我想做的是:当第一个未来完成,名单空洞时,我想到第二个表格(附索引1)。 我如何能够做到这一点?

final List<Widget> _tabs = [
const Tab(text:  Servicios ,
const Tab(text:  Profesionales , 
];

TabBar(
  isScrollable: false,
  controller: _tabController,
  tabs: _tabs,
,

TabBarView(
  controller: _tabController,
  children: [
     // TAB 0
     FutureBuilder<List<ServiceCardCommonWidget>>(
       future: widget.futureServices,
       builder: (context, snapshot) {
         // if snapshot.data.isEmpty I want to animate to the tab with index 1
       } 
     ,
     // TAB 1
     FutureBuilder<List<CoachCardCommonWidget>>(
        future: widget.futureCoaches,
        builder: (context, snapshot) { 
           // Content of Tab 1  
        },
    ,
],

,

问题回答

你们不应在建筑方法内行不通。 正如在《国际契约》中所提到的,

The future must have been obtained earlier, e.g. during State.initState, State.didUpdateWidget, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder s parent is rebuilt, the asynchronous task will be restarted.

可在<条码>initState内核对未来结果,并与所期望的Taba接洽。

 final List<Widget> _tabs = [
    const Tab(text:  Servicios ),
    const Tab(text:  Profesionales ),
  ];

  late Future<List<String>> getServices;
  late Future<List<String>> getProfessions;

  late TabController _tabController;

  @override
  void initState() {
    _tabController = TabController(length: _tabs.length, vsync: this);

    getServices = Future.delayed(const Duration(seconds: 1), () {
      final r = math.Random().nextBool();
      return r ? [ S1 ,  S2 ,  S3 ] : [];
    })
      ..then((value) => value.isEmpty ? _tabController.animateTo(1) : null);

    getProfessions =
        Future.delayed(const Duration(seconds: 1), () => [ P1 ,  P2 ,  P3 ]);
    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }





相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...

热门标签