Flutter 项目中读取 pubspec.yaml 文件时出错

问题描述 投票:0回答:1

我在尝试读取 Flutter 项目中的 pubspec.yaml 文件时遇到问题。情况是这样的:

我有以下 Dart 代码来读取 pubspec.yaml 文件:

import 'dart:io';
import 'package:yaml/yaml.dart'; // For reading YAML files

void main() async {
  // Specify the path to the pubspec.yaml file
  final filePath = '../pubspec.yaml'; // Update this according to your directory structure
  final file = File(filePath);

  // Check if the file exists
  if (await file.exists()) {
    // Read the file
    final String text = await file.readAsString();

    // Load the YAML content
    Map yaml = loadYaml(text);

    // Extract information from the YAML
    print('App Name: ${yaml['name']}');
    print('Description: ${yaml['description']}');
    print('Version: ${yaml['version']}');
    print('Author: ${yaml['author']}');
    print('Homepage: ${yaml['homepage']}');
    print('Dependencies: ${yaml['dependencies']}');
  } else {
    print('pubspec.yaml file not found: $filePath');
  }
}

当我运行此代码时,遇到以下错误消息:

Launching lib/main.dart on sdk gphone16k arm64 in debug mode... ✓ Built build/app/outputs/flutter-apk/app-debug.apk Connecting to VM Service at ws://127.0.0.1:61423/kakyQdOAGF0=/ws Connected to the VM Service. I/flutter ( 4529): Current directory: / I/flutter ( 4529): pubspec.yaml file not found at: pubspec.yaml I/.example.reader( 4529): Compiler allocated 5250KB to compile void android.view.ViewRootImpl.performTraversals() D/ProfileInstaller( 4529): Installing profile for com.example.reader

问题描述 该错误表明Dart代码在指定路径找不到pubspec.yaml文件。当前工作目录显示为根目录 (/),但文件不位于该目录。

请求帮助 我正在努力解决这个问题,并且非常感谢遇到类似问题的任何人提供的建议或指导。我可以采取哪些步骤来修复“pubspec.yaml 文件未找到”错误?任何见解将不胜感激!谢谢!

flutter yaml pubspec.yaml flutter-pubspec
1个回答
0
投票

您能描述一下您的文件夹结构吗?你的主要位置在哪里,你的 pubspec.yaml 位于哪里?

© www.soinside.com 2019 - 2024. All rights reserved.