Flutter本地存储插件sabowsla_storage的使用
Flutter本地存储插件sabowsla_storage的使用
storage-dart
是一个 Dart 客户端库,用于与 Sabowsla 存储进行交互。
文档
文档可以在官方 Sabowsla 网站上找到。
许可证
此仓库采用 MIT 许可证。
致谢
- https://github.com/sabowsla/storage-js - 由 sabowsla/storage-js 移植而来
示例代码
以下是一个完整的示例,展示了如何使用 sabowsla_storage
插件进行文件上传、下载、删除等操作。
// ignore_for_file: avoid_print
import 'dart:io';
import 'dart:typed_data';
import 'package:sabowsla_storage/sabowsla_storage.dart';
Future<void> main() async {
const sabowslaUrl = ''; // 请替换为实际的Sabowsla URL
const sabowslaKey = ''; // 请替换为实际的Sabowsla Key
final client = SabowslaStorageClient(
'$sabowslaUrl/storage/v1',
{
'Authorization': 'Bearer $sabowslaKey',
},
);
// 上传二进制文件
final List<int> listBytes = 'Hello world'.codeUnits;
final Uint8List fileData = Uint8List.fromList(listBytes);
final uploadBinaryResponse = await client.from('public').uploadBinary(
'binaryExample.txt',
fileData,
fileOptions: const FileOptions(upsert: true),
);
print('上传二进制文件响应 : $uploadBinaryResponse');
// 上传文件到桶 "public"
final file = File('example.txt');
file.writeAsStringSync('File content');
final storageResponse =
await client.from('public').upload('example.txt', file);
print('上传文件响应 : $storageResponse');
// 获取下载链接
final urlResponse =
await client.from('public').createSignedUrl('example.txt', 60);
print('下载链接 : $urlResponse');
// 下载文本文件
try {
final fileResponse = await client.from('public').download('example.txt');
print('下载的文件内容 : ${String.fromCharCodes(fileResponse)}');
} catch (error) {
print('下载文件时出错 : $error');
}
// 删除文件
final deleteResponse = await client.from('public').remove(['example.txt']);
print('删除文件ID : ${deleteResponse.first.id}');
// 清理本地文件
if (file.existsSync()) file.deleteSync();
}
代码解释
-
导入必要的包:
import 'dart:io'; import 'dart:typed_data'; import 'package:sabowsla_storage/sabowsla_storage.dart';
-
初始化客户端:
const sabowslaUrl = ''; // 请替换为实际的Sabowsla URL const sabowslaKey = ''; // 请替换为实际的Sabowsla Key final client = SabowslaStorageClient( '$sabowslaUrl/storage/v1', { 'Authorization': 'Bearer $sabowslaKey', }, );
-
上传二进制文件:
final List<int> listBytes = 'Hello world'.codeUnits; final Uint8List fileData = Uint8List.fromList(listBytes); final uploadBinaryResponse = await client.from('public').uploadBinary( 'binaryExample.txt', fileData, fileOptions: const FileOptions(upsert: true), ); print('上传二进制文件响应 : $uploadBinaryResponse');
-
上传文本文件:
final file = File('example.txt'); file.writeAsStringSync('File content'); final storageResponse = await client.from('public').upload('example.txt', file); print('上传文件响应 : $storageResponse');
-
获取下载链接:
final urlResponse = await client.from('public').createSignedUrl('example.txt', 60); print('下载链接 : $urlResponse');
-
下载文件:
try { final fileResponse = await client.from('public').download('example.txt'); print('下载的文件内容 : ${String.fromCharCodes(fileResponse)}'); } catch (error) { print('下载文件时出错 : $error'); }
-
删除文件:
final deleteResponse = await client.from('public').remove(['example.txt']); print('删除文件ID : ${deleteResponse.first.id}');
-
清理本地文件:
if (file.existsSync()) file.deleteSync();
更多关于Flutter本地存储插件sabowsla_storage的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter本地存储插件sabowsla_storage的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
sabowsla_storage
是一个用于 Flutter 的本地存储插件,它提供了一个简单的方式来在本地设备上存储和检索数据。这个插件通常用于存储小型数据,如用户偏好、设置、缓存等。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 sabowsla_storage
依赖:
dependencies:
flutter:
sdk: flutter
sabowsla_storage: ^1.0.0 # 请使用最新的版本
然后运行 flutter pub get
来安装插件。
导入插件
在你的 Dart 文件中导入 sabowsla_storage
插件:
import 'package:sabowsla_storage/sabowsla_storage.dart';
使用 sabowsla_storage
1. 初始化存储
在使用 sabowsla_storage
之前,通常需要初始化它。你可以在 main
函数中进行初始化:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SabowslaStorage.init();
runApp(MyApp());
}
2. 存储数据
你可以使用 SabowslaStorage
的 set
方法来存储数据。这个方法接受一个键和一个值:
await SabowslaStorage.setString('username', 'JohnDoe');
await SabowslaStorage.setInt('age', 25);
await SabowslaStorage.setBool('isLoggedIn', true);
3. 检索数据
你可以使用 SabowslaStorage
的 get
方法来检索数据:
String username = await SabowslaStorage.getString('username');
int age = await SabowslaStorage.getInt('age');
bool isLoggedIn = await SabowslaStorage.getBool('isLoggedIn');
4. 删除数据
你可以使用 SabowslaStorage
的 remove
方法来删除数据:
await SabowslaStorage.remove('username');
5. 清除所有数据
你可以使用 SabowslaStorage
的 clear
方法来清除所有存储的数据:
await SabowslaStorage.clear();
示例代码
以下是一个完整的示例,展示了如何使用 sabowsla_storage
来存储和检索用户数据:
import 'package:flutter/material.dart';
import 'package:sabowsla_storage/sabowsla_storage.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SabowslaStorage.init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: StorageExample(),
);
}
}
class StorageExample extends StatefulWidget {
[@override](/user/override)
_StorageExampleState createState() => _StorageExampleState();
}
class _StorageExampleState extends State<StorageExample> {
String _username = '';
[@override](/user/override)
void initState() {
super.initState();
_loadUsername();
}
Future<void> _loadUsername() async {
String username = await SabowslaStorage.getString('username') ?? 'No username';
setState(() {
_username = username;
});
}
Future<void> _saveUsername(String username) async {
await SabowslaStorage.setString('username', username);
_loadUsername();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sabowsla Storage Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Username: $_username'),
TextField(
onSubmitted: (value) {
_saveUsername(value);
},
decoration: InputDecoration(
hintText: 'Enter your username',
),
),
],
),
),
);
}
}