pq_network 0.0.10 pq_network: ^0.0.10 copied to clipboard
A new Flutter package.
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pq_network/pq_network.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
BaseOptions options = BaseOptions(
connectTimeout: 20000,
receiveTimeout: 30000,
sendTimeout: 30000,
headers: {'Content-Type': 'application/json'});
PQNetwork.config(options);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: [
CupertinoButton(child: Text("GET"), onPressed: _get),
CupertinoButton(
child: Text("GET AND DECODER"), onPressed: _getAndDecoder),
CupertinoButton(child: Text("POST"), onPressed: _post),
CupertinoButton(child: Text("DELETE"), onPressed: _delete),
CupertinoButton(child: Text("PUT"), onPressed: _put),
CupertinoButton(child: Text("DOWNLOAD"), onPressed: _download),
CupertinoButton(child: Text("GET_BYTES"), onPressed: _getBytes),
Row(
children: [
Text("BytesImage ${imageBytes.isNotEmpty}"),
if (imageBytes.isNotEmpty)
Image.memory(
imageBytes,
width: 30,
height: 30,
)
],
)
],
),
);
}
_get() async {
final path = "https:/xxxxx";
final res = await PQNetwork.get(
path,
mock: true,
mockData: {"msg": "OK", "data": {}, "code": 0},
);
print("xxxxxxxxxx get res $res");
}
_getAndDecoder() async {
final path = "https://xxxxx";
final res = await PQNetwork.get(
path,
decoder: (data) => MockDataEntity.fromJson(data),
mock: true,
mockData: {"msg": "OK", "data": {}, "code": 0},
);
print("xxxxxxxxxx get res $res");
}
_post() async {
final res = await PQNetwork.post("https://post",
mock: true, mockData: "aaaaaaaaaa");
print("xxxxxxxxxx post res $res");
}
_delete() async {
final res = await PQNetwork.post("https://delete",
mock: true, mockData: "aaaaaaaaaa");
print("xxxxxxxxxx delete res $res");
}
_put() async {
final res =
await PQNetwork.post("https://put", mock: true, mockData: "aaaaaaaaaa");
print("xxxxxxxxxx put res $res");
}
_download() async {
Directory tempDir = await getTemporaryDirectory();
String savePath = tempDir.path + "/111.jpeg";
// String savePath = "./example/flutter/111.jepg";
final res = await PQNetwork.download(
"https://img1.baidu.com/it/u=3988894411,24839006&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500",
savePath);
print("xxxxxxxxxxxxxxxx res $savePath");
}
Uint8List imageBytes = Uint8List(0);
_getBytes() async {
final res = await PQNetwork.getBytes(
"https://img1.baidu.com/it/u=3988894411,24839006&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500");
setState(() {
imageBytes = Uint8List.fromList(res.data ?? []);
});
}
}
class MockDataEntity {
String? msg;
int? code;
dynamic data;
MockDataEntity({this.data, this.code, this.msg});
MockDataEntity.fromJson(Map<String, dynamic> json) {
msg = json['msg'];
code = json['code'];
data = json['data'];
}
}