【问题标题】:asset loading for flutter_driver testsflutter_driver 测试的资产加载
【发布时间】:2020-03-19 08:58:34
【问题描述】:

我遵循了Images are not visible during flutter_driver tests的指导。

通过这个实现,我使用screenshots 得到以下调用flutter driver


[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: inheritFromWidgetOfExactType(DefaultAssetBundle) or inheritFromElement() was called before MyAppState.initState() completed.
When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget.
Typically references to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.
#0      StatefulElement.inheritFromElement.<anonymous closure> (package:flutter/src/widgets/framework.dart:4164:9)
#1      StatefulElement.inheritFromElement (package:flutter/src/widgets/framework.dart:4207:6)
#2      Element.inheritFromWidgetOfExactTyp<…>

在目标模拟器 (IOS) 上,无论是否使用CachingAssetBundle,屏幕都会变黑,没有内容。

应用程序在flutter run 下运行良好。该应用程序有 25MB 的数据通过

加载
String data = await DefaultAssetBundle.of(context).loadString(
        "assets/data/repeats.json",
        cache: false);

【问题讨论】:

    标签: flutter flutter-test


    【解决方案1】:

    已解决。我的应用正在从主应用 init_state() 内部执行 loadString 资产。

    这适用于应用运行时,但不适用于flutter_driver 环境。

    【讨论】:

    • 它是如何解决的?你在哪里加载了字符串而不是“init_state()”来让它在flutter_driver环境中工作?
    • 我在 main.dart 的 build() 函数中添加了一个调用,它实现了文件的 loadString。我在initState() 中还有一个变量,用于跟踪文件是否已加载。我想确保数据只加载一次。恕我直言,这适用于应用程序但不适用于测试环境,这是测试系统中的一个错误,但我没有提交它。
    猜你喜欢
    • 2018-09-03
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    相关资源
    最近更新 更多