Flutter:有可能在构建方法完成之前获取Future数据吗?

时间:2019-11-19 15:24:29

标签: flutter dart

import 'package:flutter/material.dart';
import 'package:hello_world/ui/food_show_case.dart';
import 'custom_appbar.dart';

class RestaurantsMenu extends StatefulWidget {
  var result;


  RestaurantsMenu({this.result});

  @override
  _RestaurantsMenuState createState() => _RestaurantsMenuState(result);
}

class _RestaurantsMenuState extends State<RestaurantsMenu> {
  var result;
  var identifier = new Map();
  Future getData() async {
    await result.then((rep) {
      identifier = rep;
    });
  }

  @override
  void initState() {
    super.initState();

    getData();
  }

  _RestaurantsMenuState(this.result);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
      child: Container(
        child: ListView(
          children: <Widget>[
            CustomAppBar(),
            tab(identifier, identifier.keys.length),
          ],
        ),
      ),
    ));
  }

  Widget tab(Map map, int longueur) {
    return SingleChildScrollView(
      child: Container(
        height: 560,
        width: double.infinity,
        child: DefaultTabController(
          initialIndex: 0,
          length: longueur,
          child: Scaffold(
            appBar: AppBar(
              elevation: 0.0,
              backgroundColor: Colors.transparent,
              bottom: PreferredSize(
                preferredSize: Size.fromHeight(65),
                child: Container(
                  color: Colors.transparent,
                  child: SafeArea(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        TabBar(
                          isScrollable: true,
                          labelPadding: EdgeInsets.only(top: 15),
                          indicatorColor: Colors.transparent,
                          labelColor: Colors.black,
                          labelStyle: TextStyle(
                            fontSize: 25,
                            fontWeight: FontWeight.w800,
                            fontFamily: 'slabo',
                          ),
                          unselectedLabelColor: Colors.black26,
                          unselectedLabelStyle: TextStyle(
                            fontSize: 25,
                            fontWeight: FontWeight.w800,
                            fontFamily: 'slabo',
                          ),
                          tabs: <Widget>[
                            for (var elem in map.keys)
                              Container(
                                padding: EdgeInsets.only(left: 40),
                                child: Text(elem),
                              ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            body: TabBarView(
              children: <Widget>[
                for (var tabu in map.keys)
                  FoodShowCase(
                    map: identifier,
                    tabofnow: tabu,
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

这是错误: 抛出了另一个异常:'package:flutter / src / material / tab_controller.dart':断言失败:316行pos 15:'initialIndex> = 0 && initialIndex

如果我没记错的话,问题是Flutter尝试使用未来数据的长度(这些数据是我从Firebase检索到的一堆信息)的长度来构建选项卡控制器,但是构建方法已经领先它(当我在收到此错误几秒钟后执行热重装时,一切正常并且错误消失了),并且无法生成它。我可以暂时停止或暂停它,直到到达所需数据后再恢复它?有人可以帮我吗?谢谢

0 个答案:

没有答案