fl_dio 0.2.1 fl_dio: ^0.2.1 copied to clipboard
Extended dio and added three interceptors and the JsonParse component.
import 'dart:convert';
import 'package:fl_dio/fl_dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =
GlobalKey<ScaffoldMessengerState>();
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void main() {
/// 必须设置 DebuggerInterceptorHelper
DebuggerInterceptorHelper().navigatorKey = navigatorKey;
/// 设置JsonParse字体颜色
JsonParse.color = JsonParseColor();
ExtendedDio().initialize(interceptors: [
/// 日志打印
LoggerInterceptor(),
/// debug 调试工具
DebuggerInterceptor(),
/// cookie 保存和获取
CookiesInterceptor()
]);
runApp(MaterialApp(
navigatorKey: navigatorKey,
debugShowCheckedModeBanner: false,
home: const Scaffold(body: HomePage()),
));
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
child: Wrap(
runSpacing: 12,
spacing: 12,
alignment: WrapAlignment.center,
children: [
ElevatedText('show', onPressed: DebuggerInterceptorHelper().show),
ElevatedText('showDebugger',
onPressed: DebuggerInterceptorHelper().showDebugger),
ElevatedText('hide', onPressed: DebuggerInterceptorHelper().hide),
ElevatedText('get', onPressed: get),
ElevatedText('post', onPressed: post),
ElevatedText('put', onPressed: put),
ElevatedText('delete', onPressed: delete),
ElevatedText('patch', onPressed: patch),
ElevatedText('download', onPressed: download),
ElevatedText('upload', onPressed: upload),
ElevatedText('JsonParse', onPressed: () {
showCupertinoModalPopup(
context: context, builder: (_) => const JsonParsePage());
}),
]),
);
}
void get() async {
await ExtendedDio().get(
'https://lf3-beecdn.bytetos.com/obj/ies-fe-bee/bee_prod/biz_216/bee_prod_216_bee_publish_6676.json');
}
void post() async {
showSnackBar('未添加');
}
void put() async {
showSnackBar('未添加');
}
void delete() async {
showSnackBar('未添加');
}
void patch() async {
showSnackBar('未添加');
}
void download() async {}
void upload() async {
showSnackBar('未添加');
}
void showSnackBar(String text) {
scaffoldMessengerKey.currentState
?.showSnackBar(SnackBar(content: Text(text)));
}
}
class ElevatedText extends ElevatedButton {
ElevatedText(String text, {required super.onPressed, super.key})
: super(child: Text(text));
}
class JsonParsePage extends StatelessWidget {
const JsonParsePage({super.key});
@override
Widget build(BuildContext context) {
final Map<dynamic, dynamic> map =
jsonDecode('{"name":"BeJson","url":"http://www.bejson.com",'
'"page":88,"num":88.88,"isNonProfit":true,"address":'
'{"street":"科技园路.","city":"江苏苏州","country":"中国"},'
'"links":[{"name":"Google","url":"http://www.google.com"},'
'{"name":"Baidu","url":"http://www.baidu.com"},'
'{"name":"SoSo","url":"http://www.SoSo.com"}]}')
as Map<dynamic, dynamic>;
final List<dynamic> list = jsonDecode(
'[{"name":"Google","url":"http://www.google.com"},{"name":"Baidu",'
'"url":"http://www.baidu.com"},{"name":"SoSo","url":"http://www.SoSo.com"}]')
as List<dynamic>;
return Scaffold(
appBar: AppBar(title: const Text('JsonParse')),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children: [
const Text('JsonParse'),
Card(child: JsonParse(map)),
const Text('JsonParse.list'),
Card(child: JsonParse.list(list))
]),
)));
}
}