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

如何解决Flutter:类型'Future <dynamic>'不是类型'Widget'的子类型// MaterialPageRoute <dynamic>null

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

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,),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,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

解决方法

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-