简体   繁体   English

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

[英]Flutter app can't load Json file while being run with flutter_driver

I'm trying to setup my Flutter app to automate screenshots using flutter_driver and integration tests, but the app is stuck at the point where it should await a future for a *.json file.我正在尝试设置我的 Flutter 应用程序以使用flutter_driver和集成测试自动进行屏幕截图,但该应用程序卡在了它应该等待 *.json 文件的未来的地步。
Does anybody know how to fix that?有人知道如何解决吗?

Steps to Reproduce重现步骤

  1. Setup an app that needs to read a Json File (I used this method: String myJson = await rootBundle.loadString('my_resources/myjson.json');设置一个需要读取 Json 文件的应用程序(我使用了这个方法: String myJson = await rootBundle.loadString('my_resources/myjson.json');
  2. Set up a flutter_driver test:设置一个flutter_driver测试:

App.dart:应用程序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: 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. Run the test with flutter drive --target=test_driver/app.dart使用flutter drive --target=test_driver/app.dart

In my case the app is stuck at the await future for the *.json file from 1. Using the regular flutter run command everything works as expected.在我的情况下,应用程序卡在等待从1.开始的*.json文件的未来。使用常规的flutter run命令,一切都按预期工作。

flutter doctor -v flutter 博士-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!

Has anybody a solution for that?有没有人解决这个问题? The *.json I want to import is a config file, so it's critical for me to import it for a smooth running.我要导入的*.json是一个配置文件,因此导入它以顺利运行对我来说至关重要。 Would be really glad about any workaround.任何解决方法都会非常高兴。

There is an outstanding issue for this on Flutter GitHub.在 Flutter GitHub 上存在一个未解决的问题。 It appears there is an issue with the VM and an isolate in Flutter Driver. VM 和 Flutter 驱动程序中的隔离似乎存在问题。

See this comment https://github.com/flutter/flutter/issues/30641#issuecomment-524868136 it has a link to a fork of Flutter with an example of a test (here https://github.com/vishna/flutter/commit/2f858fc334fc2f43b92c318d02bdbf1ad0984033 ) that has solved this sort of issue for me in the passed See this comment https://github.com/flutter/flutter/issues/30641#issuecomment-524868136 it has a link to a fork of Flutter with an example of a test (here https://github.com/vishna/flutter /commit/2f858fc334fc2f43b92c318d02bdbf1ad0984033 )在过去为我解决了此类问题

Also, maybe worth sharing your source code so that people can run/debug etc.此外,也许值得分享您的源代码,以便人们可以运行/调试等。

Use this code for loading JSON:使用此代码加载 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());
    }

As @George Herbert said - there is opened Flutter issue at the moment.正如@George Herbert 所说 - 目前存在 Flutter 问题。 All code running compute will hang.所有运行计算的代码都会挂起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM