【问题标题】:Loading asset using rootBundle in Flutter web application - unexected null value error在 Flutter Web 应用程序中使用 rootBundle 加载资产 - 未执行的空值错误
【发布时间】:2021-06-08 21:04:17
【问题描述】:

rootBundle.loadString($path)function 调用有问题。我想从 yaml 文件资产加载配置,并在我的 Web 应用程序 的基础上创建 Config 自定义对象。但我收到错误Unexpected null value。我处理不了,有人遇到过类似问题吗?

颤振医生

[✓] Flutter (Channel master, 2.3.0-17.0.pre.225, on Ubuntu 18.04.5 LTS 5.4.0-74-generic, locale pl_PL.UTF-8)
    • Flutter version 2.3.0-17.0.pre.225 at /opt/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2794dea0df (3 days ago), 2021-06-05 16:04:04 -0400
    • Engine revision 03d645e782
    • Dart version 2.14.0 (build 2.14.0-182.0.dev)

[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /usr/lib/android-sdk
    • Platform android-30, build-tools 29.0.3
    • ANDROID_HOME = /usr/lib/android-sdk
    • ANDROID_SDK_ROOT = /usr/lib/android-sdk
    • Java binary at: /usr/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~18.04-b10)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/linux#android-setup for more details.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
    • cmake version 3.19.2
    • ninja version 1.8.2
    • pkg-config version 0.29.1

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] VS Code (version 1.56.2)
    • VS Code at /usr/share/code
    • Flutter extension version 3.23.0

[✓] Connected device (3 available)
    • Redmi Note 8 Pro (mobile) • 4lfmlflvfms8lbd6 • android-arm64  • Android 10 (API 29)
    • Linux (desktop)           • linux            • linux-x64      • Ubuntu 18.04.5 LTS 5.4.0-74-generic
    • Chrome (web)              • chrome           • web-javascript • Google Chrome 90.0.4430.212

错误

Error: Unexpected null value.
    at Object.throw_ [as throw] (http://localhost:40177/dart_sdk.js:5056:11)
    at Object.nullCheck (http://localhost:40177/dart_sdk.js:5381:30)
    at load (http://localhost:40177/packages/flutter/src/services/system_channels.dart.lib.js:2240:33)
    at load.next (<anonymous>)
    at runBody (http://localhost:40177/dart_sdk.js:37463:34)
    at Object._async [as async] (http://localhost:40177/dart_sdk.js:37494:7)
    at asset_bundle.PlatformAssetBundle.new.load (http://localhost:40177/packages/flutter/src/services/system_channels.dart.lib.js:2238:20)
    at asset_bundle.PlatformAssetBundle.new.loadString (http://localhost:40177/packages/flutter/src/services/system_channels.dart.lib.js:2110:32)
    at loadString.next (<anonymous>)
    at runBody (http://localhost:40177/dart_sdk.js:37463:34)
    at Object._async [as async] (http://localhost:40177/dart_sdk.js:37494:7)
    at asset_bundle.PlatformAssetBundle.new.loadString (http://localhost:40177/packages/flutter/src/services/system_channels.dart.lib.js:2109:20)
    at http://localhost:40177/packages/flutter/src/services/system_channels.dart.lib.js:2191:83
    at IdentityMap.new.putIfAbsent (http://localhost:40177/dart_sdk.js:25663:21)
    at asset_bundle.PlatformAssetBundle.new.loadString (http://localhost:40177/packages/flutter/src/services/system_channels.dart.lib.js:2191:57)
    at create (http://localhost:40177/packages/egm/config/config.dart.lib.js:25:60)
    at create.next (<anonymous>)
    at runBody (http://localhost:40177/dart_sdk.js:37463:34)
    at Object._async [as async] (http://localhost:40177/dart_sdk.js:37494:7)
    at Function.create (http://localhost:40177/packages/egm/config/config.dart.lib.js:24:20)
    at main$ (http://localhost:40177/packages/egm/main.dart.lib.js:230:42)
    at main$.next (<anonymous>)
    at runBody (http://localhost:40177/dart_sdk.js:37463:34)
    at Object._async [as async] (http://localhost:40177/dart_sdk.js:37494:7)
    at main$ (http://localhost:40177/packages/egm/main.dart.lib.js:228:18)
    at main (http://localhost:40177/web_entrypoint.dart.lib.js:33:29)
    at main.next (<anonymous>)
    at http://localhost:40177/dart_sdk.js:37444:33
    at _RootZone.runUnary (http://localhost:40177/dart_sdk.js:37315:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:40177/dart_sdk.js:32550:29)
    at handleValueCallback (http://localhost:40177/dart_sdk.js:33077:49)
    at Function._propagateToListeners (http://localhost:40177/dart_sdk.js:33115:17)
    at _Future.new.[_completeWithValue] (http://localhost:40177/dart_sdk.js:32963:23)
    at http://localhost:40177/dart_sdk.js:32214:46
    at _RootZone.runUnary (http://localhost:40177/dart_sdk.js:37315:59)
    at _FutureListener.then.handleValue (http://localhost:40177/dart_sdk.js:32550:29)
    at handleValueCallback (http://localhost:40177/dart_sdk.js:33077:49)
    at Function._propagateToListeners (http://localhost:40177/dart_sdk.js:33115:17)
    at _Future.new.[_completeWithValue] (http://localhost:40177/dart_sdk.js:32963:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:40177/dart_sdk.js:32984:35)
    at Object._microtaskLoop (http://localhost:40177/dart_sdk.js:37567:13)
    at _startMicrotaskLoop (http://localhost:40177/dart_sdk.js:37573:13)
    at http://localhost:40177/dart_sdk.js:33323:9

配置类代码

import 'dart:async';
import 'package:flutter/services.dart';
import 'package:yaml/yaml.dart';

class Config {
  /// private constructor
  Config._(dynamic yamlConfig)
      : communication =
            CommunicationConfig.fromYaml(yamlConfig["communication"]);

  /// Factory method
  static Future<Config> create(final String path) async {
    dynamic yamlRawConfig = await rootBundle.loadString(path); //! ERROR
    dynamic yamlConfig = loadYaml(yamlRawConfig);
    return Config._(yamlConfig);
  }

  CommunicationConfig communication;
}

class CommunicationConfig {
  CommunicationConfig.fromYaml(dynamic communication)
      : address = communication["address"];
  String address = "";
}

在 main 中创建配置

void main() async {
  dynamic app = MyApp();
  Config config = await Config.create("assets/config.yaml");
  Communication(messagesHandler: MessagesHandler(), config: config);
  runApp(app);
}

【问题讨论】:

    标签: flutter config assets


    【解决方案1】:

    您需要在 main 函数的开头调用 WidgetsFlutterBinding.ensureInitialized()

    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      // Your code here...
      runApp(MyApp());
    }
    

    【讨论】:

      【解决方案2】:

      对于您想要做的工作,您需要确保 pubspec.yaml 具有正确声明的必要颤振资产

      【讨论】:

      • 如果这是解决方案,请不要提问。如果这只是评论,请将其删除,因为此空间仅供解决方案使用
      猜你喜欢
      • 1970-01-01
      • 2013-03-14
      • 2019-01-07
      • 2015-08-29
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      相关资源
      最近更新 更多