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

    原文作者:程序猿的玻璃心
    原文地址: https://blog.csdn.net/mo_feng_/article/details/85104013
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞