Xdg_directories: A Flutter package for XDG paths

xdg_directories

A Dart package for reading XDG directory configuration information on Linux.

Getting Started

On Linux, xdg is a system developed by freedesktop.org, a project to work on interoperability and shared base technology for free software desktop environments for Linux.

This Dart package can be used to determine the directory configuration information defined by xdg, such as where the Documents or Desktop directories are. These are called "user directories" and are defined in configuration file in the user's home directory.

See this wiki for more details of the XDG Base Directory implementation.

To use this package, the basic XDG values for the following are available via a Dart API:

dataHome - The single base directory relative to which user-specific data files should be written. (Corresponds to $XDG_DATA_HOME).

configHome - The a single base directory relative to which user-specific configuration files should be written. (Corresponds to $XDG_CONFIG_HOME).

dataDirs - The list of preference-ordered base directories relative to which data files should be searched. (Corresponds to $XDG_DATA_DIRS).

configDirs - The list of preference-ordered base directories relative to which configuration files should be searched. (Corresponds to $XDG_CONFIG_DIRS).

cacheHome - The base directory relative to which user-specific non-essential (cached) data should be written. (Corresponds to $XDG_CACHE_HOME).

runtimeDir - The base directory relative to which user-specific runtime files and other file objects should be placed. (Corresponds to $XDG_RUNTIME_DIR).

getUserDirectoryNames() - Returns a set of the names of user directories defined in the xdg configuration files.

getUserDirectory(String dirName) - Gets the value of the user dir with the given name. Requesting a user dir that doesn't exist returns null. The dirName argument is case-insensitive. See this wiki for more details and what values of dirName might be available.

Use this package as a library

Depend on it

Run this command:

With Dart:

 $ dart pub add xdg_directories

With Flutter:

 $ flutter pub add xdg_directories

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):

dependencies:
  xdg_directories: ^1.0.3

Alternatively, your editor might support dart pub get or flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:xdg_directories/xdg_directories.dart';

example/lib/main.dart

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:xdg_directories/xdg_directories.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'XDG Directories Demo',
      home: MyHomePage(title: 'XDG Directories Demo'),
      color: Colors.blue,
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final Set<String> userDirectoryNames = getUserDirectoryNames();
  String selectedUserDirectory = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: ListView(
          padding: const EdgeInsets.only(left: 20),
          shrinkWrap: true,
          children: <Widget>[
            const SizedBox(
              height: 20,
            ),
            ListView.builder(
              shrinkWrap: true,
              itemCount: userDirectoryNames.length,
              itemBuilder: (BuildContext context, int index) => Text(
                '${userDirectoryNames.elementAt(index)}: \n${getUserDirectory(userDirectoryNames.elementAt(index))?.path}\n',
              ),
            ),
            Text('Data Home: \n${dataHome.path}\n'),
            Text('Config Home: \n${configHome.path}\n'),
            Text(
                'Data Directories: \n${dataDirs.map((Directory directory) => directory.path).toList().join('\n')}\n'),
            Text(
                'Config Directories: \n${configDirs.map((Directory directory) => directory.path).toList().join('\n')}\n'),
            Text('Cache Home: \n${cacheHome.path}\n'),
            Text('Runtime Directory: \n${runtimeDir?.path}\n'),
            const SizedBox(
              height: 100,
            ),
          ],
        ),
      ),
    );
  }
}

Download details:

Author: flutter
Source: https://github.com/flutter/packages

License: BSD-3-Clause license

#flutter #dart 

Xdg_directories: A Flutter package for XDG paths
1.80 GEEK