在flutter的fish redux中构建组件时,构建函数返回null

时间:2021-01-03 15:57:06

标签: flutter

当我在 fish-redux 中使用 buildComponent 时:

 buildArticle(Item item) {
    viewService.buildComponent("articlepg");
  }

显示此错误:

flutter: [debug] Capture from onError A build function returned null.
The offending widget is:
  Builder-[GlobalKey#319ce]
Build functions must never return null.
To return an empty space that causes the building widget to fill available room, return "Container()". To return an empty space that takes as little room as possible, return "Container(width: 0.0, height: 0.0)".

======== Exception caught by widgets library =======================================================
The following assertion was thrown building Builder-[GlobalKey#319ce](dirty):
A build function returned null.

The offending widget is: Builder-[GlobalKey#319ce]
Build functions must never return null.

To return an empty space that causes the building widget to fill available room, return "Container()". To return an empty space that takes as little room as possible, return "Container(width: 0.0, height: 0.0)".

The relevant error-causing widget was: 
  MaterialApp file:///Users/dolphin/Documents/GitHub/cruise-open/lib/src/widgets/CruiseApp.dart:28:12
When the exception was thrown, this was the stack: 
#0      debugWidgetBuilderValue.<anonymous closure> (package:flutter/src/widgets/debug.dart:304:7)
#1      debugWidgetBuilderValue (package:flutter/src/widgets/debug.dart:325:4)
#2      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4686:7)
#3      Element.rebuild (package:flutter/src/widgets/framework.dart:4375:5)
#4      StatelessElement.update (package:flutter/src/widgets/framework.dart:4762:5)
...
====================================================================================================

这是我的连接器代码:

class ArticlePgConnector extends ConnOp<ArticleListState, ArticlePgState> {
  @override
  ArticlePgState get(ArticleListState state) {
    ArticlePgState articlePageState = state.articlePgState.clone();
    articlePageState.article = new Item();
    return articlePageState;
  }

  @override
  void set(ArticleListState state, ArticlePgState subState) {
    //state.articleListState.articles = subState.articles;
  }
}

这是 view.dart 构建代码:

import 'package:Cruise/src/common/Repo.dart';
import 'package:Cruise/src/models/Item.dart';
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(ArticlePgState state, Dispatch dispatch, ViewService viewService) {
  Item item = new Item();
  item.id = 1112.toString();
  item.title ="ddd";
  var showToTopBtn = state.showToTopBtn;
  PageStorageBucket pageStorageBucket = state.pageStorageBucket;
  ScrollController scrollController;
  Map<String, ScrollController> scrollControllers = state.scrollControllers;
  scrollController = scrollControllers[item.id];

  scrollController.addListener(() => {
    if (scrollController.offset < 1000)
      {showToTopBtn = false}
    else if (scrollController.offset >= 1000)
      {showToTopBtn = true}
  });

  return PageStorage(
      bucket: pageStorageBucket,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Cruise'),
          actions: [
            if (item.parent != null)
              IconButton(
                icon: Icon(Feather.corner_left_up),
                onPressed: () async {
                  Item parent = await Repo.fetchArticleItem(item.parent);
                },
              ),
          ],
        ),
        body: NotificationListener<ScrollNotification>(
          onNotification: (ScrollNotification sn) {},
          child: CustomScrollView(
            key: PageStorageKey(item.id),
            controller: scrollController,
            slivers: [
              /*SliverToBoxAdapter(
                  child: StoryInformation(
                item: item,
              )),*/
              //CommentList(item: item),
            ],
          ),
        ),
        floatingActionButton: !showToTopBtn
            ? null
            : FloatingActionButton(
            child: Icon(Icons.arrow_upward),
            onPressed: () {
              if (scrollController.hasClients) {
                scrollController.animateTo(.0,
                    duration: Duration(milliseconds: 200),
                    curve: Curves.ease);
              }
            }),
      ));
}

当我调试这段代码时,它没有输入 view.dart 代码。我该怎么做才能修复它?

0 个答案:

没有答案