Flutter:类型'Future <dynamic>'不是类型'Widget'的子类型// MaterialPageRoute <dynamic>(null))

时间:2020-09-10 20:14:37

标签: flutter dynamic flutter-animation flutter-futurebuilder

我试图导航到相机屏幕,当我触发导航时,出现错误

Another exception was thrown: type 'Future<dynamic>' is not a subtype of type 'Widget'
Another exception was thrown: setState() or markNeedsBuild() called during build.

错误指向我上一页的导航。

我也在那看到了这个错误

The AnimationController notifying status listeners was: AnimationController#a911e(⏮ 0.000; paused; for MaterialPageRoute<dynamic>(null))
onTap: () => Navigator.push(
   context,
  MaterialPageRoute(
    builder: (context) => CameraScreen())),

这是发生错误的功能

captureImageWithCamera() async {
    Navigator.pop(this.context);
    File imageFile = await ImagePicker.pickImage(
      source: ImageSource.camera,
      maxHeight: 680,
      maxWidth: 970,
    );
    if (imageFile != null) {
      //imageFile = await cropImage(imageFile);
      setState(() {
        //this.file = imageFile as File;
        print(file);
      });
      await controller.dispose();
    }
  }

仅出于全文考虑,这是第一页的完整构建器,该构建器调用导航到相机屏幕。您将在其中看到到CameraScreen的导航。

createStoryView() {
    return FutureBuilder(
      future: usersReference.document(currentOnlineUserId).get(),
      builder: (context, dataSnapshot) {
        if (!dataSnapshot.hasData) {
          return circularProgress();
        } else {
          User user = User.fromDocument(dataSnapshot.data);
          return Material(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(0.0),
                  child: Card(
                    color: Colors.white,
                    elevation: 0.0,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: ListTile(
                        leading: Stack(
                          children: <Widget>[
                            GestureDetector(
                              child: Padding(
                                padding: EdgeInsets.only(top: 0.0, bottom: 0.0),
                                child: user.photoUrl.isNotEmpty
                                    ? CircleAvatar(
                                        radius: 30.0,
                                        backgroundColor: Colors.grey,
                                        backgroundImage:
                                            CachedNetworkImageProvider(
                                                user.photoUrl),
                                      )
                                    : CircleAvatar(
                                        radius: 30.0,
                                        backgroundColor: Colors.grey,
                                      ),
                              ),
                              onTap: () => Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) => StoryPageView())),
                            ),
                          ],
                        ),
                        title: Text(
                          user.profileName,
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                        subtitle: Text("Add a Story"),
                        onTap: () => Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => CameraScreen())),
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    "Viewed Stories",
                    style: TextStyle(
                      color: Colors.grey,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    padding: const EdgeInsets.all(0.0),
                    color: Colors.white,
                    child: ListView(
                      children: <Widget>[
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: ListTile(
                            leading: CircleAvatar(
                              radius: 30.0,
                              backgroundColor: Colors.grey,
                            ),
                            title: Text(
                              "Username",
                              style: TextStyle(
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            subtitle: Text("Timestamp"),
                            onTap: () => Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => StoryPageView())),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          );
        }
      },
    );
  }

这是CameraPage的构建器

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Colors.blue),
        title: Text(
          "Story Post",
          style: TextStyle(
            color: Colors.blue,
            fontSize: 22.0,
          ),
        ),
      ),
      body: file == null ? captureImageWithCamera() : displayStoryUploadScreen(),
    );
  }

这是完整的错误消息

══╡ EXCEPTION CAUGHT BY ANIMATION LIBRARY ╞═════════════════════════════════════════════════════════
The following assertion was thrown while notifying status listeners for AnimationController:
setState() or markNeedsBuild() called during build.
This _ModalScope<dynamic> widget cannot be marked as needing to build because the framework is
already in the process of building widgets.  A widget can be marked as needing to be built during
the build phase only if one of its ancestors is currently building. This exception is allowed
because the framework builds parent widgets before children, which means a dirty descendant will
always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was:
  _ModalScope<dynamic>-[LabeledGlobalKey<_ModalScopeState<dynamic>>#0c8ee]
The widget which was currently being built when the offending call was made was:
  CameraScreen
When the exception was thrown, this was the stack:
[38;5;244m#0      Element.markNeedsBuild.<anonymous closure>[39;49m
[38;5;244m#1      Element.markNeedsBuild[39;49m
[38;5;244m#2      State.setState[39;49m
[38;5;244m#3      _ModalScopeState._routeSetState[39;49m
[38;5;244m#4      ModalRoute.setState[39;49m
[38;5;244m#5      ModalRoute.changedInternalState[39;49m
[38;5;244m#6      TransitionRoute._handleStatusChanged[39;49m
[38;5;244m#7      AnimationLocalStatusListenersMixin.notifyStatusListeners[39;49m
[38;5;244m#8      AnimationController._checkStatusChanged[39;49m
[38;5;244m#9      AnimationController._animateToInternal[39;49m
[38;5;244m#10     AnimationController.reverse[39;49m
[38;5;244m#11     TransitionRoute.didPop[39;49m
[38;5;244m#12     LocalHistoryRoute.didPop[39;49m
[38;5;244m#13     _RouteEntry.pop[39;49m
[38;5;244m#14     NavigatorState.pop[39;49m
[38;5;244m#15     Navigator.pop[39;49m
[38;5;248m#16     _CameraScreenState.captureImageWithCamera[39;49m
[38;5;248m#17     _CameraScreenState.build[39;49m
[38;5;244m#18     StatefulElement.build[39;49m
[38;5;244m#19     ComponentElement.performRebuild[39;49m
[38;5;244m#20     StatefulElement.performRebuild[39;49m
[38;5;244m#21     Element.rebuild[39;49m
[38;5;244m#22     ComponentElement._firstBuild[39;49m
[38;5;244m#23     StatefulElement._firstBuild[39;49m
[38;5;244m#24     ComponentElement.mount[39;49m
...     Normal element mounting (24 frames)
[38;5;244m#48     Element.inflateWidget[39;49m
[38;5;244m#49     MultiChildRenderObjectElement.mount[39;49m
...     Normal element mounting (136 frames)
[38;5;244m#185    Element.inflateWidget[39;49m
[38;5;244m#186    Element.updateChild[39;49m
[38;5;244m#187    RenderObjectElement.updateChildren[39;49m
[38;5;244m#188    MultiChildRenderObjectElement.update[39;49m
[38;5;244m#189    Element.updateChild[39;49m
[38;5;244m#190    ComponentElement.performRebuild[39;49m
[38;5;244m#191    StatefulElement.performRebuild[39;49m
[38;5;244m#192    Element.rebuild[39;49m
[38;5;244m#193    StatefulElement.update[39;49m
[38;5;244m#194    Element.updateChild[39;49m
[38;5;244m#195    ComponentElement.performRebuild[39;49m
[38;5;244m#196    Element.rebuild[39;49m
[38;5;244m#197    ProxyElement.update[39;49m
[38;5;244m#198    _InheritedNotifierElement.update[39;49m
[38;5;244m#199    Element.updateChild[39;49m
[38;5;244m#200    SingleChildRenderObjectElement.update[39;49m
[38;5;244m#201    Element.updateChild[39;49m
[38;5;244m#202    ComponentElement.performRebuild[39;49m
[38;5;244m#203    StatefulElement.performRebuild[39;49m
[38;5;244m#204    Element.rebuild[39;49m
[38;5;244m#205    StatefulElement.update[39;49m
[38;5;244m#206    Element.updateChild[39;49m
[38;5;244m#207    SingleChildRenderObjectElement.update[39;49m
[38;5;244m#208    Element.updateChild[39;49m
[38;5;244m#209    SingleChildRenderObjectElement.update[39;49m
[38;5;244m#210    Element.updateChild[39;49m
[38;5;244m#211    ComponentElement.performRebuild[39;49m
[38;5;244m#212    Element.rebuild[39;49m
[38;5;244m#213    StatelessElement.update[39;49m
[38;5;244m#214    Element.updateChild[39;49m
[38;5;244m#215    ComponentElement.performRebuild[39;49m
[38;5;244m#216    Element.rebuild[39;49m
[38;5;244m#217    ProxyElement.update[39;49m
[38;5;244m#218    Element.updateChild[39;49m
[38;5;244m#219    ComponentElement.performRebuild[39;49m
[38;5;244m#220    StatefulElement.performRebuild[39;49m
[38;5;244m#221    Element.rebuild[39;49m
[38;5;244m#222    BuildOwner.buildScope[39;49m
[38;5;244m#223    WidgetsBinding.drawFrame[39;49m
[38;5;244m#224    RendererBinding._handlePersistentFrameCallback[39;49m
[38;5;244m#225    SchedulerBinding._invokeFrameCallback[39;49m
[38;5;244m#226    SchedulerBinding.handleDrawFrame[39;49m
[38;5;244m#227    SchedulerBinding._handleDrawFrame[39;49m
[38;5;244m#231    _invoke  (dart:ui/hooks.dart:253:10)[39;49m
[38;5;244m#232    _drawFrame  (dart:ui/hooks.dart:211:3)[39;49m
(elided 3 frames from dart:async)
The AnimationController notifying status listeners was:
  AnimationController#ac0aa(⏮ 0.000; paused; for MaterialPageRoute<dynamic>(null))
════════════════════════════════════════════════════════════════════════════════════════════════════

[38;5;248m════════ Exception caught by animation library ═════════════════════════════════[39;49m
[38;5;244mThe following assertion was thrown while notifying status listeners for AnimationController:[39;49m
setState() or markNeedsBuild() called during build.

[38;5;244mThis _ModalScope<dynamic> widget cannot be marked as needing to build because the framework is already in the process of building widgets.  A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.[39;49m
[38;5;244mThe widget on which setState() or markNeedsBuild() was called was: _ModalScope<dynamic>-[LabeledGlobalKey<_ModalScopeState<dynamic>>#0c8ee][39;49m
    [38;5;244mstate: _ModalScopeState<dynamic>#b0964[39;49m
[38;5;244mThe widget which was currently being built when the offending call was made was: CameraScreen[39;49m
    [38;5;244mdirty[39;49m
    [38;5;244mstate: _CameraScreenState#54b04[39;49m
[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0      Element.markNeedsBuild.<anonymous closure>[39;49m
[38;5;244m#1      Element.markNeedsBuild[39;49m
[38;5;244m#2      State.setState[39;49m
[38;5;244m#3      _ModalScopeState._routeSetState[39;49m
[38;5;244m#4      ModalRoute.setState[39;49m
[38;5;244m...[39;49m
[38;5;244mThe AnimationController notifying status listeners was: AnimationController#ac0aa(⏮ 0.000; paused; for MaterialPageRoute<dynamic>(null))[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
Another exception was thrown: type 'Future<dynamic>' is not a subtype of type 'Widget'

[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
type 'Future<dynamic>' is not a subtype of type 'Widget'
[38;5;244mThe relevant error-causing widget was[39;49m
    [38;5;248mCameraScreen[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m

1 个答案:

答案 0 :(得分:1)

builder的{​​{1}}函数应返回Widget或来自MaterialPageRouteStatelessWidget的impimpentes类,显然StatefullWidget都不是这些。

您的CameraScreen确实会返回captureImageWithCamera,但是Future<void>中的body必须是小部件,也不清楚{{1}的返回类型是什么},而且必须是小部件。

如果您需要未来的价值来构建窗口小部件,则需要使用FutureBuilder来等待它,而无需查看渲染树。