【问题标题】:How to resolve Error: The getter 'body1' isn't defined for the class 'TextTheme' in flutter?如何解决错误:没有为 Flutter 中的“TextTheme”类定义吸气剂“body1”?
【发布时间】:2021-11-17 14:00:07
【问题描述】:

我的 Flutter 应用程序无法在 2.5.0-5.3.pre beta 版本上运行。之后我搜索了它,然后需要升级我的颤振 sdk,所以已经升级到最新的 beta 版本2.6.0-5.2.pre,但仍然出现错误。 place_picker-0.9.19-nullsafety 插件的错误。并且无法解决。

错误日志:

../../../../Workspace/flutter/.pub-cache/hosted/pub.dartlang.org/place_picker-0.9.19-nullsafety/lib/widgets/rich_suggestion.dart:35:89: Error: The getter 'body1' isn't defined for the class 'TextTheme'.
 - 'TextTheme' is from 'package:flutter/src/material/text_theme.dart' ('../../../../Workspace/flutter/packages/flutter/lib/src/material/text_theme.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'body1'.
      TextSpan(text: boldText, style: style.copyWith(color: Theme.of(context).textTheme.body1!.color)),
                                                                                        ^^^^^
../../../../Workspace/flutter/.pub-cache/hosted/pub.dartlang.org/place_picker-0.9.19-nullsafety/lib/widgets/search_input.dart:60:65: Error: The getter 'body1' isn't defined for the class 'TextTheme'.
 - 'TextTheme' is from 'package:flutter/src/material/text_theme.dart' ('../../../../Workspace/flutter/packages/flutter/lib/src/material/text_theme.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'body1'.
          Icon(Icons.search, color: Theme.of(context).textTheme.body1!.color),
                                                                ^^^^^


FAILURE: Build failed with an exception.

* Where:
Script '/Users/brightroots/Workspace/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1005

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command '/Users/brightroots/Workspace/flutter/bin/flutter'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 31s

错误:

我目前的 FUTTER 版本:

Flutter 2.6.0-5.2.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 400608f101 (8 days ago) • 2021-09-15 15:50:26 -0700
Engine • revision 1d521d89d8
Tools • Dart 2.15.0 (build 2.15.0-82.2.beta)

【问题讨论】:

  • 改用bodyText1
  • @Irsvmb 你是说,我必须编辑插件?!好吧,让我试试吧。
  • @Irsvmb 谢谢,它有效。其实我从来没有尝试过编辑任何插件,总是害怕这样做。
  • 作者真的应该在编辑它。 :)

标签: flutter dart flutter-dependencies


【解决方案1】:

编辑 dart 包中的行,如下所示,它将起作用。当然作者应该这样做,但你也可以自己编辑:)

rich_suggestion.dart

原文:TextSpan(text: boldText, style: style.copyWith(color: Theme.of(context).textTheme.body1!.color)),

新:TextSpan(text: boldText, style: style.copyWith(color: Theme.of(context).textTheme.bodyText1!.color)),

search_input.dart

原文:Icon(Icons.search, color: Theme.of(context).textTheme.body1!.color),

新:Icon(Icons.search, color: Theme.of(context).textTheme.bodyText1!.color),

【讨论】:

    【解决方案2】:

    我正在处理这个错误。就我而言,这是因为存在与 Flutter 2.5 不兼容的依赖项。我尝试了很多解决方案。但只有这对我有用。

    首先将其添加到您的 pubspec.yaml,如上所述 this link.

    如果您有错误提示:remote end hung up unexpectedly while git cloning

    然后运行this git commands

    这解决了我的问题。

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    【解决方案3】:

    日志上引发的错误似乎是由您正在使用的插件引发的:place_picker-0.9.19-nullsafety。日志表明问题来自Theme.of(context).textTheme.body1

    查看 Flutter 文档,TextTheme 上的 body1 定义为 bodyText1。您可能需要考虑更新在 TextTheme 中调用 body1 的方式。否则,您可以在 GitHub 上跟踪与此问题相关的 issue ticket

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 1970-01-01
      • 2020-09-23
      • 2021-02-27
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      相关资源
      最近更新 更多