搜索插件、代码仓与 APP 分类

插件详情v2.0.6

fluri

Fluri 是一个流畅的 URI 库,旨在让 URI 修改变得简单。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3万
喜欢数
6
Pub 分数
140

安装插件

flutter pub add fluri

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Fluri 是一个流畅的 URI 库,旨在简化 Dart 和 Flutter 应用程序中的 URI 操作。它提供了一个直观、可链式调用的 API,轻松构建、修改和解析 URI。开发者可以无缝地添加路径、查询参数和片段,同时保持代码简洁易读。由 Workiva 开发,Fluri 支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,非常适合需要强大 URL 处理功能的跨平台项目。

适合场景

  • 构建动态 API 端点
  • 在运行时解析和修改URL
  • 构造复杂的查询字符串
  • 处理相对和绝对 URI
  • 使用各种 URL 测试 HTTP 客户端
  • 使用参数管理导航路线

主要特点

  • 流畅的、可链式调用的 API
  • 支持查询参数变异
  • 轻松路径和片段操作
  • 跨平台兼容性
  • 不可变的 URI 构造
  • 强大的解析和序列化

适合对象

  • 需要灵活的 URI 处理的 Flutter 开发人员
  • 需要动态端点生成的API客户端
  • 具有复杂路由或深度链接的应用程序
  • 优先考虑干净、可维护的 URL 逻辑的团队
  • 针对多个平台的项目

使用建议

  • 不适用于低级别URL解析
  • 需要理解 URI 结构
  • 可能会为简单的静态URL增加开销
  • 仅限 URI 特定操作
  • 最好与 dio 或 http 等 HTTP 客户端配合使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

fluri

Pub Build Status documentation

Examples/Usage

Fluri is a fluent URI library for Dart built to make URI mutation easy.

The dart:core.Uri class provides an immutable representation of URIs, which makes it difficult to incrementally build them or update them at a later time. If you wanted to build a long URI from the individual pieces, you would do something like this:

var uri = Uri(
  scheme: 'https',
  host: 'example.com',
  path: 'path/to/resource'
);

If you later wanted to update the path and add a query parameter, you'd have to do this:

uri = uri.replace(
  path: 'new/path',
  query: 'foo=true'
);

Now let's say you want update the query without losing what you already have:

var query = Map.from(uri.queryParameters);
query['bar'] = '10';
uri = uri.replace(queryParameters: query);

As you can see, incremental or fluent-style URI mutations become a hassle with the core Uri class.

With fluri, the above interactions are easy:

import 'package:fluri/fluri.dart';

var fluri = Fluri()
  ..scheme = 'https'
  ..host = 'example.com'
  ..path = 'path/to/resource';

fluri
  ..path = 'new/path'
  ..query = 'foo=true';

fluri.updateQuery({'bar': '10'});

Additional methods like appendToPath and setQueryParam make it easy to build on top of a base URL:

import 'package:fluri/fluri.dart';

var base = Fluri('https://example.com/base/');

var fluri = Fluri.from(base)
  ..appendToPath('path/to/resource')
  ..setQueryParam('count', '10');

Fluri also supports multi-value parameters. To access the query parameters as Map<String, List<String>>, use queryParametersAll (just like the Uri class):

var fluri = Fluri('/resource?format=json&format=text');
print(fluri.queryParameters); // {'format': 'text'}
print(fluri.queryParametersAll); // {'format': ['json', 'text']}

To set a single query parameter to multiple values:

var fluri = Fluri('/resource');
fluri.setQueryParam('format', ['json', 'text']);
print(fluri.queryParametersAll); // {'format': ['json', 'text']}

Using setQueryParam will always replace existing values:

var fluri = Fluri('/resource');
fluri.setQueryParam('format', ['json', 'text']);
fluri.setQueryParam('format', ['binary', 'text']);
print(fluri.queryParametersAll); // {'format': ['binary', 'text']}

You can use the queryParametersAll setter to set the entire query with multi-value param support:

var fluri = Fluri('/resource');
fluri.queryParametersAll = {'format': ['json', 'text'], 'count': ['5']}
print(fluri.queryParametersAll); // {'format': ['json', 'text'], 'count': ['5']}

Again, if you need to preserve existing query parameters, you can use the updateQuery method to do so. Set mergeValues: true and any values that you provide will be merged with existing values:

var fluri = Fluri('/resource?format=json');
fluri.updateQuery({'format': ['binary', 'text'], 'count': '5'}, mergeValues: true);
print(fluri.queryParametersAll); // {'format': ['binary', 'json', 'text'], 'count': ['5']}

所属分类

源码仓库

使用此插件的项目