【问题标题】:Difference Between void main() => runApp(MaterialApp(home:MyApp())); and void main() => runApp(MyApp());void main() => runApp(MaterialApp(home:MyApp())); 之间的区别和 void main() => runApp(MyApp());
【发布时间】:2020-07-28 13:27:40
【问题描述】:

如果我替换,这是我在 Flutter 中的凸起按钮代码

void main() => runApp(MaterialApp(home:MyApp()));

void main() => runApp(MyApp());

它抛出一个错误

给我一​​个解决方案......它与flutter和flutter插件的版本有什么联系吗?

谁能提供这两行之间的区别?

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home:MyApp()));


class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
          child: RaisedButton(
            child: Text("Hello"),
            onPressed: (){
              print("Hello world");
            },
          )
      ),
    );
  }
}

错误:

Performing hot restart...
Syncing files to device SM G965F...
Restarted application in 1,759ms.
I/flutter (22269): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (22269): The following assertion was thrown building InkWell(gestures: [tap], clipped to BoxShape.rectangle,
I/flutter (22269): dirty, state: _InkResponseState<InkResponse>#db17e):
I/flutter (22269): No Directionality widget found.
I/flutter (22269): InkWell widgets require a Directionality widget ancestor.
I/flutter (22269): The specific widget that could not find a Directionality ancestor was:
I/flutter (22269):   InkWell
I/flutter (22269): The ownership chain for the affected widget is: "InkWell ← DefaultTextStyle ←
I/flutter (22269):   AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#cee82 ink renderer] ←
I/flutter (22269):   NotificationListener<LayoutChangedNotification> ← CustomPaint ← _ShapeBorderPaint ← PhysicalShape
I/flutter (22269):   ← _MaterialInterior ← Material ← ⋯"
I/flutter (22269): Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the
I/flutter (22269): top of your application widget tree. It determines the ambient reading direction and is used, for
I/flutter (22269): example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve
I/flutter (22269): EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.
I/flutter (22269): 
I/flutter (22269): The relevant error-causing widget was:
I/flutter (22269):   RaisedButton file:///F:/Flutter_Projects/flutter_test_app/lib/main.dart:16:18
I/flutter (22269): 
I/flutter (22269): When the exception was thrown, this was the stack:
I/flutter (22269): #0      debugCheckHasDirectionality.<anonymous closure> (package:flutter/src/widgets/debug.dart:247:7)
I/flutter (22269): #1      debugCheckHasDirectionality (package:flutter/src/widgets/debug.dart:263:4)
I/flutter (22269): #2      InkResponse.debugCheckContext (package:flutter/src/material/ink_well.dart:521:12)
I/flutter (22269): #3      _InkResponseState.build (package:flutter/src/material/ink_well.dart:843:19)
I/flutter (22269): #4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
I/flutter (22269): #5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
I/flutter (22269): #6      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
I/flutter (22269): #7      Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
I/flutter (22269): #8      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4481:5)
I/flutter (22269): #9      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4666:11)
I/flutter (22269): #10     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
I/flutter (22269): ...     Normal element mounting (92 frames)
I/flutter (22269): #102    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
I/flutter (22269): #103    Element.updateChild (package:flutter/src/widgets/framework.dart:3214:18)
I/flutter (22269): #104    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1148:16)
I/flutter (22269): #105    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1119:5)
I/flutter (22269): #106    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1061:17)
I/flutter (22269): #107    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2607:19)
I/flutter (22269): #108    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1060:13)
I/flutter (22269): #109    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:941:7)
I/flutter (22269): #110    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:922:7)
I/flutter (22269): (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
I/flutter (22269): 
I/flutter (22269): ════════════════════════════════════════════════════════════════════════════════════════════════════

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    MaterialApp 建立在WidgetApp 之上。这些类用于提供一些基础的 Flutter Widget 作为导航处理(仅作为示例)。

    错误说:

    I/flutter (22269): No Directionality widget found.
    

    在 RaisedButton 小部件中使用的 InkWell 需要它,这是 WidgetApp 提供的小部件之一。这就是为什么如果您不将应用程序封装在其中,很多事情将无法正常工作。

    您可以找到更多关于WidgetApp的信息here

    MaterialApp 用于提供材料设计的外观和感觉。

    【讨论】:

      【解决方案2】:

      由于 SDK 的某些限制,您无法将 StatefulWidget 传递给 runApp(),因此最好提供 StatelessWidget,然后在其中传递 StatefulWidget

      【讨论】:

        猜你喜欢
        • 2018-12-21
        • 2012-08-26
        • 1970-01-01
        • 1970-01-01
        • 2011-04-12
        • 2018-03-16
        • 2020-03-12
        • 1970-01-01
        • 2013-11-04
        相关资源
        最近更新 更多