Flutter常见库使用
1、网络库 dio
dio: ^5.4.0
import 'package:dio/dio.dart'; final dio = Dio(); void getHttp() async { final response = await dio.get('https://dart.dev'); print(response); }
2、JSON解析
json_serializable: ^6.7.1
json_annotation: ^4.8.1
build_runner: ^2.4.7
新建一个模型 test_model.dart
import 'package:json_annotation/json_annotation.dart'; part 'test_model.g.dart'; @JsonSerializable() class TestModel { String name = ""; int age = 0; double height = 0; TestModel({required this.name, required this.age, required this.height}); factory TestModel.fromJson(Map<String, dynamic> json) => _$TestModelFromJson(json); Map<String, dynamic> toJson() => _$TestModelToJson(this); }
终端执行
dart run build_runner build
3、获取文件路径
path_provider: ^2.1.1
void _downlodUrl() async { var url = 'https://sample-videos.com/video123/flv/240/big_buck_bunny_240p_10mb.flv'; var path = await getDownloadsDirectory(); print("ios paht:$path"); String savePath = "${path?.path}/temp.mp4"; print(savePath); Dio().download(url, savePath,onReceiveProgress: (c,t) { var progress = "${(c / t * 100)}%"; print(progress); }); }
4、数据存储
shared_preferences: ^2.2.2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // Obtain shared preferences. final SharedPreferences prefs = await SharedPreferences.getInstance(); // Save an integer value to 'counter' key. await prefs.setInt( 'counter' , 10); // Save an boolean value to 'repeat' key. await prefs.setBool( 'repeat' , true ); // Save an double value to 'decimal' key. await prefs.setDouble( 'decimal' , 1.5); // Save an String value to 'action' key. await prefs.setString( 'action' , 'Start' ); // Save an list of strings to 'items' key. await prefs.setStringList( 'items' , <String>[ 'Earth' , 'Moon' , 'Sun' ]); //获取 // Try reading data from the 'counter' key. If it doesn't exist, returns null. final int ? counter = prefs.getInt( 'counter' ); // Try reading data from the 'repeat' key. If it doesn't exist, returns null. final bool ? repeat = prefs.getBool( 'repeat' ); // Try reading data from the 'decimal' key. If it doesn't exist, returns null. final double ? decimal = prefs.getDouble( 'decimal' ); // Try reading data from the 'action' key. If it doesn't exist, returns null. final String? action = prefs.getString( 'action' ); // Try reading data from the 'items' key. If it doesn't exist, returns null. final List<String>? items = prefs.getStringList( 'items' ); // Remove data for the 'counter' key.await prefs.remove('counter'); |
5.图片快速加载
flutter_gen_runner: ^5.3.2
yaml中添加
build_runner: ^2.4.7 flutter_gen_runner: ^5.3.2
yaml添加图片路径
assets:
- Asset/Image/
导入库后执行命令
dart run build_runner build
更新图片
flutter clean
dart run build_runner build
使用
Assets.asset.image.img1.image()
6.打开相册、相机
image_picker: ^1.0.6
在项目中添加相应的权限
如果运行报错、先用xdode运行程序、把权限授权了在运行
void openAlbum() async{ final ImagePicker picker = ImagePicker(); // Pick an image. final XFile? image = await picker.pickImage(source: ImageSource.gallery); } }
7.局部刷新框架 getX
get: ^4.6.6
1.修改 MaterialApp 为
GetMaterialApp
2.Controller继承与GetXController
class MinePageController extends GetxController { //GetX改造步骤3:给变量值添加.obs var_backgroundurl = Assets.image.defaultPhotopath.obs String get backgroundUrl => _backgroundUrl.value set backgroundUrl(String url) => _backgroundUrlvalue = url
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2016-12-27 Xcode 8 Swift 类似插件方法