Flutter错误和解决办法

Flutter使用遇到的问题:
1,Expected a key while parsing a block mapping

Error on line 30, column 4 of pubspec.yaml: Expected a key while parsing a block mapping.
   assets:
   ^

异常原因:assets 前面多了个空格
解决方案:去除 assets 前面的一个空格,注意整体的标签对齐
2,RichText widgets require a Directionality widget ancestor.
很奇怪的错误,因为昨天并没有这样的问题,今天出现了,具体如下:

I/flutter (20611): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (20611): The following assertion was thrown building Text("xxx", inherit: true, color: Color(0xff2b6dea),
I/flutter (20611): size: 22.0):
I/flutter (20611): No Directionality widget found.
I/flutter (20611): RichText widgets require a Directionality widget ancestor.
I/flutter (20611): The specific widget that could not find a Directionality ancestor was:
I/flutter (20611):   RichText(softWrap: wrapping at box width, maxLines: unlimited, text: "xxx")
I/flutter (20611): The ownership chain for the affected widget is:
I/flutter (20611):   RichText ← Text ← Column ← Padding ← DecoratedBox ← Container ← Login ← [root]
I/flutter (20611): Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the
I/flutter (20611): top of your application widget tree. It determines the ambient reading direction and is used, for
I/flutter (20611): example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve
I/flutter (20611): EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.

到目前为止不知道是什么导致的?
解决办法是:

  原代码:
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color(0xFFFFFFFF),
      padding: EdgeInsets.fromLTRB(0.0, 78.0, 0.0, 5.0),
      child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
          Image.asset("images/logo.png", width: 48.0, ),
          new Container(height: 5.0),
          Text(
            "Helloword",
            style: TextStyle(
              color: Color(0xFF2B6DEA),
              fontSize: 22.0,
            ),
          ),
           ],
        ),
    );
  }
解决问题的代码:
@override
  Widget build(BuildContext context) {
    return Container(
      color: Color(0xFFFFFFFF),
      padding: EdgeInsets.fromLTRB(0.0, 78.0, 0.0, 5.0),
      child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
          Image.asset("images/logo.png", width: 48.0, ),
          new Container(height: 5.0),
          Text(
            "Helloword",
            style: TextStyle(
              color: Color(0xFF2B6DEA),
              fontSize: 22.0,
            ),
     		textDirection: TextDirection.ltr
          ),
           ],
        ),
    );
  }

关键是下面这句代码:(不明觉厉)
textDirection: TextDirection.ltr
3,TextField widgets require a Material widget ancestor.

I/flutter (20611): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (20611): The following assertion was thrown building TextField(decoration: InputDecoration(hintText:
I/flutter (20611): "输入手机号码", prefixIcon: Icon(IconData(U+0E7FD))), autofocus: true, autocorrect: true, max length
I/flutter (20611): enforced, onTap: null, dirty, state: _TextFieldState#8027d):
I/flutter (20611): No Material widget found.
I/flutter (20611): TextField widgets require a Material widget ancestor.
I/flutter (20611): In material design, most widgets are conceptually "printed" on a sheet of material. In Flutter's
I/flutter (20611): material library, that material is represented by the Material widget. It is the Material widget
I/flutter (20611): that renders ink splashes, for instance. Because of this, many material library widgets require that
I/flutter (20611): there be a Material widget in the tree above them.
I/flutter (20611): To introduce a Material widget, you can either directly include one, or use a widget that contains
I/flutter (20611): Material itself, such as a Card, Dialog, Drawer, or Scaffold.
I/flutter (20611): The specific widget that could not find a Material ancestor was:
I/flutter (20611):   TextField(decoration: InputDecoration(hintText: "输入手机号码", prefixIcon: Icon(IconData(U+0E7FD))),
I/flutter (20611):   autofocus: true, autocorrect: true, max length enforced, onTap: null)
I/flutter (20611): The ancestors of this widget were:
I/flutter (20611):   Column(direction: vertical, mainAxisAlignment: start, crossAxisAlignment: center)
I/flutter (20611):   Padding(padding: EdgeInsets(0.0, 78.0, 0.0, 5.0))
I/flutter (20611):   DecoratedBox(bg: BoxDecoration(color: Color(0xffffffff)))
I/flutter (20611):   Container(padding: EdgeInsets(0.0, 78.0, 0.0, 5.0), bg: BoxDecoration(color: Color(0xffffffff)))
I/flutter (20611):   Login
I/flutter (20611):   [root]

TextField widget需要被包裹在Scaffold widget中,否则会报错 textfield widgets require a material widget ancestor
4, No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of()

I/flutter (20611): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (20611): The following assertion was thrown building Login:
I/flutter (20611): MediaQuery.of() called with a context that does not contain a MediaQuery.
I/flutter (20611): No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
I/flutter (20611): This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce
I/flutter (20611): a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
I/flutter (20611): The context used was:
I/flutter (20611):   Scaffold(dirty, state: ScaffoldState#9a79e(lifecycle state: initialized, tickers: tracking 1
I/flutter (20611):   ticker))

造成的原因是,我直接从main.dart的runApp跳转到一个StatefulWidget,而该StatefulWidget返回的是Scaffold
5,Finished with error: FormatException: Bad UTF-8 encoding 0xe4 (at offset 9)
这是由于app的版本为release找不到keystore文件

Finished with error: FormatException: Bad UTF-8 encoding 0xe4 (at offset 9)

检查android–app–build.gradle里的signingConfigs是否正确
6,Because xxx requires SDK version 2.0.0-dev.68.0 3.0.0, version solving failed.
Flutter错误
下载github里的Flutter项目,运行之后,报如下错误

The current Dart SDK version is 2.0.0-dev.58.0.flutter-f981f09760.
Because sample_catalog requires SDK version >=2.0.0-dev.68.0 <3.0.0, version solving failed.

从字面意思,大概就是说Dart的版本是2.0的,但项目需要大于2.0的Dart版本。
这个错误导致的原因是,依赖的包,或者自己项目本身就是用了高版本的dart.
我的解决办法是:
AndroidStudio – Tools – Flutter – Flutter Upgrade
运行结果如下(日志缩减了些)

Upgrading Flutter from C:\Users\Hzx\flutter...
From https://github.com/flutter/flutter
   c7ea3ca377..5391447fae  beta       -> origin/beta
 * [new branch]            gallery    -> origin/gallery
 * [new branch]            maryx-animated-container -> origin/maryx-animated-container
   5413560415..aca3aba10e  master     -> origin/master
 * [new branch]            stable     -> origin/stable
 * [new tag]               v1.1.5     -> v1.1.5
Checking out files:  10% (239/2211)   
Checking out files: 100% (2211/2211), done.
Updating c7ea3ca377..5391447fae
 .../app/src/main/res/mipmap-mdpi/ic_launcher.png   |   Bin
 .../android}/gradle.properties                     |     0
 .../src/main/res/mipmap-hdpi/ic_launcher.png       |   Bin 0 -> 544 bytes
 .../common}/.idea/modules.xml.tmpl                 |     0
 .../LaunchImage.imageset/LaunchImage@3x.png        |   Bin 0 -> 68 bytes
 2140 files changed, 204402 insertions(+), 95793 deletions(-)
 mode change 100644 => 100755
Upgrading engine...
Checking Dart SDK version...
Downloading Dart SDK from Flutter engine 7375a0f414bde4bc941e623482221db2fc8c4ab5...
Unzipping Dart SDK...
Building flutter tool...
Running pub upgrade...
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Downloading package sky_engine...                                0.6s
Downloading common tools...                                      1.8s

运行完之后,发现红色提示都消失了。
这个时候可以run项目,发现项目正在下载包。
可以看到“Dart Packages/ Dart SDK/ Flutter Plugins”等SDK包
第一次运行会比较久
当然,还有一种方法,就是自己手动下载Flutter,下载地址是:https://github.com/flutter/flutter

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当在Android Studio中切换Flutter SDK之后,有可能会遇到一些报错。以下是一些可能的报错以及它们的解决方法: 1. 缺少Flutter插件:如果Android Studio无法找到Flutter插件,请确保已正确安装Flutter插件。要安装插件,可以打开Android Studio的“设置”菜单,然后导航到“插件”部分。在该部分中,搜索“Flutter”,然后单击“安装”按钮。 2. 缺少相关依赖项:切换Flutter SDK可能会导致一些依赖项缺失。可以尝试运行“flutter doctor”命令来检查缺失的依赖项并解决它们。该命令将列出任何缺失的依赖项并提供修复建议。 3. Flutter版本冲突:如果Flutter SDK的版本与项目中所需的Flutter版本不兼容,则可能会发生冲突。在这种情况下,可以尝试更新或降低Flutter SDK的版本来与项目需求相匹配。可以通过在终端中运行“flutter upgrade”来更新Flutter SDK,并降级到特定版本可以使用“flutter version <version>”命令。 4. 缺少Flutter配置:有时候在切换Flutter SDK后,项目可能需要重新配置。请确保在项目的根目录中运行“flutter create .”命令,以重新生成Flutter配置文件。这将确保项目正确识别和使用新的Flutter SDK。 5. 其他错误:如果以上步骤无法解决问题,则可能存在其他错误。在这种情况下,建议查看错误消息以获取更具体的信息,并搜索相关错误来找到解决办法。可以在Flutter社区论坛、GitHub问题页面等地方寻求帮助,以获取更多定制化的错误解决方案。 总的来说,切换Flutter SDK可能会带来一些问题,但通过检查插件安装、缺失依赖项、版本冲突、重新配置项目以及查找特定错误的解决方法,通常可以解决这些问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值