第三方库安装过程
- 在pubspec.yaml 中添加相应的库
- 安装依赖(put get | flutter packages get | vsCode 中 保存一下,会自动下载)
- 引入 (eg:
import 'package:shared_preferences/shared_preferences.dart';)
- 使用 (请参考 pub.dev 中示例 )
dio 网络请求
dio 官网地址, dio相当于axios,用于发起请求
代码示例:
class Body extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(10),
child: ElevatedButton(
onPressed: () {
getIp();
},
child: Text('点击发送请求'),
),
);
}
void getIp() async {
try {
final url = 'https://httpbin.org/ip';
Response res = await Dio().get(url);
String ip = res.data['origin'];
print(ip);
} catch (e) {
print(e);
}
}
}