【问题标题】:opening page multiple times bug多次打开页面错误
【发布时间】:2019-04-29 04:51:05
【问题描述】:

未来的构建器会多次运行并同时打开多个页面。如何解决此错误?

SubCategory StatefulWidget

  class subCategory extends StatefulWidget {
  final int RegId;
  final int AssetId;
  final String title;
  final int ParentId;
  final int equipmentId;

  subCategory(this.RegId, this.AssetId, this.title, this.ParentId,this.equipmentId, {Key key})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _mainCategory();
}

_mainCategory 状态小部件

class _mainCategory extends State<subCategory> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: submitRequestAppBar(context),
      body: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.grey[350],
            leading: Container(),
            title: Text(
              widget.title,
              textAlign: TextAlign.left,
              style:  TextStyle(fontWeight: FontWeight.bold, color: Colors.black),),),

FutureBuilder 内部

          body: FutureBuilder(
            future: getRegister1(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.none:
                  return Text('Press button to start.');
                case ConnectionState.active:
                case ConnectionState.waiting:
                  return waitingCircle();
                case ConnectionState.done:
                  if (snapshot.hasError) return Text('Error: ${snapshot.error}');
                  if (snapshot.data.length == 0)
                    return noResultFound();
                  else
                    return createListView(context, snapshot); }
              return null; // unreachable
            },)),); }

CreateListView 小部件

  Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
    List<SubCategoryItem> values = snapshot.data;

返回列表视图

    return ListView.builder(
      padding: EdgeInsets.only(top: 8.0, right: 0.0, left: 0.0),
      itemCount: 1,
      itemBuilder: (BuildContext context, int index) {
        return GridView.count(
          physics: ScrollPhysics(),
          shrinkWrap: true,
          crossAxisCount: 4,

          children: List.generate(values.length, (index) {

返回 GridTile

            return GridTile(
              child: GestureDetector(
                onTap: () => Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => WoDescription2(
                            widget.RegId,
                            widget.AssetId,
                            widget.ParentId,
                            values[index].childId,
                            false,
                            1,
                            widget.title,
                        widget.equipmentId))),

列小部件

                child: Column(
                  children: [
                    Card(
                      child: Container(
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: Colors.blueAccent, width: 1.5)),
                        child: Stack(
                          children: <Widget>[

flutter_svg 插件用于 svg 图像

                            SvgPicture.asset('assets/images/Defect/icon-${values[index].childId}.svg',
                              height: 50.0,),],),), ),
                    Expanded(
                      child: Text(
                        '${values[index].description}',
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: 10.0),),),],),), ); }),); },);}

FutureBuider 未来:getRegister1

  Future getRegister1() async {
    List<SubCategoryItem> description = [];
    List cat = [];

    var catLocal = (await HelperDatabase1().displayDefCatRelation());
    var defCatLocal = (await HelperDatabase1().display());

    for (int i = 0; i < catLocal.length; i++) {
      if (widget.RegId == catLocal[i].r &&
          widget.AssetId == catLocal[i].t &&
          widget.ParentId == catLocal[i].p) {
        cat.add(catLocal[i].c);}}

    for (int i = 0; i < cat.length; i++) {
      for (int j = 0; j < defCatLocal.length; j++) {
        if (cat[i] == defCatLocal[j].deF_CAT_ID) {

          var oneItem = SubCategoryItem(
              childId: defCatLocal[j].deF_CAT_ID,
              description: defCatLocal[j].description);
          await description.add(oneItem);}}}
    return description;}}

SubCategoryItem 类

class SubCategoryItem {
  int childId;String description;
  SubCategoryItem({this.childId, this.description});}

【问题讨论】:

  • 为什么在 description.add(oneItem) 上需要“等待”?这不是你 build() 方法被多次调用的原因吗?
  • 我现在检查。我删除了等待,但没有任何改变

标签: dart flutter flutter-layout


【解决方案1】:

只需在 initState 上声明 Future out de Build 方法/

_mainCategory 状态小部件

class _mainCategory extends State<subCategory> {
  Future _futureData;

  @override
  void initState() {
      super.initState();    
      _futureData = getRegister1();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(

FutureBuilder 内部

body: FutureBuilder(
  future: _futureData,
     builder: (BuildContext context, AsyncSnapshot snapshot) {

【讨论】:

  • 您能否详细说明您的答案,可能带有示例和解释?
  • 在 initState() 上声明未来的 getRegister1() 并用 var 替换 futureBuilder 上的未来。 `主体:FutureBuilder(未来:varFuture,`
  • @Acrueid 我在initState() 上添加了getRegister()。但是我如何从FutureBuilder( future: 调用getRegister()
  • @Arcrueid 感谢您尝试帮助我。我现在做到了。但 getRegister() 仍然运行多次。
  • 我为 1.future: _futureData 和 2.Future getRegister1() async{ 添加了调试点,首先运行getRegister1(),然后运行future: _futureData,然后在屏幕出现后再次运行两次getRegister1()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
相关资源
最近更新 更多