【问题标题】:Flutter : "Could not resolved the package" every install new packagesFlutter:每次安装新包时“无法解析包”
【发布时间】:2020-05-21 02:52:48
【问题描述】:

我在从 pub.dev 安装新软件包时遇到了一些问题。我用这个命令在我的根项目中创建了我自己的包:

flutter create --template=package main_template

结构文件夹

我在 ma​​in_template 而不是 lib 中安装新包。每次在 ma​​in_template 中安装新包我都会收到此错误:


Compiler message:
Error: Could not resolve the package 'device_info' in 'package:device_info/device_info.dart'.
sinergi_flutter_template/main_template/lib/variable/get_device_id.dart:3:8: Error: Not found: 'package:device_info/device_info.dart'
import 'package:device_info/device_info.dart';
       ^
sinergi_flutter_template/main_template/lib/variable/get_device_id.dart:9:11: Error: 'DeviceInfoPlugin' isn't a type.
    final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
          ^^^^^^^^^^^^^^^^
sinergi_flutter_template/main_template/lib/variable/get_device_id.dart:9:47: Error: The method 'DeviceInfoPlugin' isn't defined for the class 'GetDeviceId'.
 - 'GetDeviceId' is from 'package:main_template/variable/get_device_id.dart' ('sinergi_flutter_template/main_template/lib/variable/get_device_id.dart').
Try correcting the name to the name of an existing method, or defining a method named 'DeviceInfoPlugin'.
    final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
                                              ^^^^^^^^^^^^^^^^
U
nhandled exception:
FileSystemException(uri=org-dartlang-u
ntranslatable-uri:package%3Adevice_info%2Fdevice_info.dart; message=StandardFileSystem only supports file:* and data:* URIs)
#0      StandardFileSystem.entityForUri  (package:front_end/src/api_prototype/standard_file_system.dart:33:7)
#1      asFileUri ( (package:vm/kernel_front_end.dart:604:3)
7)
#2      writeDepfile  (package:vm/kernel_front_end.dart:799:21)
<asynchronous suspension>
#3      FrontendCompiler.compile  (package:frontend_server/frontend_server.dart:472:15)
<asynchronous suspension>
#4      _FlutterFrontendCompiler.compile (package:flut
ter_frontend_server/server.dart:38:22)
#5      starter  (package:flutter_frontend_server/server.dart:149:27)
#6      main  (file:///C:/b/s/w/ir/cache/builder/src/flutter/flutter_frontend_server/bin/starter.dart:8:30)
#7      _startIsolate.<anonymous closure>  (dart:isolate-patch/isolate_patch.dart:305:32)
#8      _RawReceivePortImpl._handleMessage  (dart:isolate-patch/isolate_patch.dart:174:12)
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
build failed.

FAILURE: Build failed with an exception.

* Where:
Script 'C:\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 780

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 20s
Gradle task assembleDebug failed with exit code 1
Exited (sigterm)

直到现在我尝试安装 3 个包 [缓存网络图像、设备信息、闪屏]。所有的包都给我这个错误。

我该如何解决?

【问题讨论】:

  • 您是否已将软件包添加到您的 pubspec 文件并运行 flutter pub get
  • 不要将 Flutter 项目放在其他 Flutter 项目中。他们应该是兄弟姐妹;您的应用程序和包将处于同一级别,例如 projects。在其中一个的 pubspec 中,使用 path: ../other 引用另一个。
  • @Abion47 在运行flutter pub get 之后一切正常!我认为使用flutter packages get 就足够了,但是在关注您的评论之后是有效的。
  • @Richard Heap 所以我的main_template 应该在我的根项目之外?
  • 是的,因为这样您就可以在以后的项目中重复使用它。

标签: flutter dart


【解决方案1】:

你在用 vs 代码吗?

只需重启或关闭 vs code 应用程序(所有窗口),然后再次打开运行

颤振酒吧获取

【讨论】:

    【解决方案2】:

    这个命令解决了我的问题:

    颤振酒吧获取

    【讨论】:

      【解决方案3】:

      您在使用 Android Studio 吗?

      然后重新启动它。为我工作。

      确保您已经运行:

      颤振酒吧获取

      【讨论】:

        【解决方案4】:

        我和你有同样的问题。就我而言,我有 2 个模块(包即 coreshared)。在shared 模块中,我添加了一个新包,例如shimmercached_network_image,用于创建自定义小部件。另外,我可以import 并使用我已经添加到shared 模块中的那个包。但是在我运行项目之后,我收到了类似Could not resolved the package ... 的错误信息。所以要修复它,我只需将shimmercached_network_image 包添加到我的根项目中。所以为此,我已经添加了2x 包。在shared 模块和根项目中。因此,我认为如果遇到该错误,您也应该这样做。

        在我的情况下,需要 2x 将新包添加到 pubspec.yml 是 json_serializableshimmercached_network_image

        并非所有的都必须添加两次,只添加那些导致错误的。

        【讨论】:

          【解决方案5】:

          从您的项目中删除 .idea 文件夹并重新启动 android studio 对我来说效果很好

          【讨论】:

            【解决方案6】:

            安装characters package 解决了我的问题。

            【讨论】:

              猜你喜欢
              • 2020-08-15
              • 2022-06-14
              • 2021-08-02
              • 2020-03-07
              • 2015-06-23
              • 1970-01-01
              • 1970-01-01
              • 2018-03-06
              • 1970-01-01
              相关资源
              最近更新 更多