Flutter应用程序在通过flutter_driver运行时无法加载Json文件

时间:2019-09-24 09:49:42

标签: android ios flutter flutter-test

我正在尝试将我的Flutter应用设置为使用flutter_driver和集成测试来自动执行屏幕截图,但是该应用处于应等待* .json文件的将来。
有人知道如何解决这个问题吗?

复制步骤

  1. 设置需要读取Json文件的应用程序(我使用此方法:String myJson = await rootBundle.loadString('my_resources/myjson.json');
  2. 设置flutter_driver测试:

App.dart:

import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter_project/main.dart' as app;

void main() {
  // This line enables the extension.
  enableFlutterDriverExtension();

  // Call the `main()` function of the app, or call `runApp` with
  // any widget you are interested in testing.
  app.main();
}

App_test.dart:

import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';

void main() {
  group('MyApp', () {
    FlutterDriver driver;

    setUpAll(() async {
      driver = await FlutterDriver.connect();
    });

    // Close the connection to the driver after the tests have completed.
    tearDownAll(() async {
      if (driver != null) {
        driver.close();
      }
    });
  });
}
  1. 使用flutter drive --target=test_driver/app.dart
  2. 运行测试

对于我来说,该应用在1.中的 *。json 文件停留在等待的将来 使用常规的flutter run命令,一切正常。

flutter doctor -v

[✓] Flutter (Channel stable, v1.9.1+hotfix.2, on Mac OS X 10.14.6 18G95, locale de-DE)
    • Flutter version 1.9.1+hotfix.2 at /Users/patrick/Development/flutter
    • Framework revision 2d2a1ffec9 (2 weeks ago), 2019-09-06 18:39:49 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0


[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/patrick/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 10.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.3, Build version 10G8
    • CocoaPods version 1.7.5

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 39.0.3
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.38.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.4.1

[✓] Connected device (2 available)
    • iPhone 7  • 5E5C912E-EA1D-438B-B5C5-E963D4BF9B33 • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-4 (simulator)
    • iPhone Xʀ • EE0825D6-B8B4-4010-B954-CC913953F5D4 • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-4 (simulator)

• No issues found!

有人对此有解决方案吗?我要导入的 *。json 是一个配置文件,因此对我来说,导入该文件以使其运行平稳至关重要。真的很高兴有任何解决方法。

2 个答案:

答案 0 :(得分:1)

在Flutter GitHub上有一个未解决的问题。看来VM和Flutter Driver中的隔离存在问题。

请参见此评论https://github.com/flutter/flutter/issues/30641#issuecomment-524868136,其中包含指向Flutter分支的链接,其中包含一个测试示例(此处为https://github.com/vishna/flutter/commit/2f858fc334fc2f43b92c318d02bdbf1ad0984033),该示例已通过测试为我解决了此类问题

另外,也许值得分享您的源代码,以便人们可以运行/调试等。

答案 1 :(得分:-1)

使用以下代码加载JSON:

static Future<String> getJson(String file) async
    {
        final ByteData data = await rootBundle.load(file);
        if (data == null) return null;
        return utf8.decode(data.buffer.asUint8List());
    }

正如@George Herbert所说-目前有未解决的Flutter问题。 所有运行compute的代码都将挂起。