Flutter http请求库dio介绍
fish-redux之 adapter基础使用
正常我们使用dio处理网络请求时,都是放在init方法中进行实现,那么在fish_redux中如何拿到init方法呢,其实无论是在effect或者reducer中我们都可以通过注册监听对象Lifecycle.initState拿到对应的init回调,不过笔者认为,对于reducer应该是处理state相关的数据,相反的effect处理非state事件,因此在effect 中注册对应方法,处理网络请求
实现效果
Untitled4.gif
代码示例如下
- service网络请求
class Service {
static Dio dio = Dio(BaseOptions(
connectTimeout: 30000,
receiveTimeout: 30000,
baseUrl: API.base_url,
contentType: ContentType.parse(
"application/x-www-form-urlencoded; charset=utf-8")));
static Future post(
String path, {
data,
Map queryParameters,
Options options,
CancelToken cancelToken,
ProgressCallback onSendProgress,
ProgressCallback onReceiveProgress,
}) {
return dio
.post(path,
queryParameters: queryParameters,
options: options,
data: data,
cancelToken: cancelToken,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress)
.then((value) => value.data);
}
}
class ServiceHepher {
static Future getExampleList() =>
Service.post(API.home_list).then((json) => ExampleModel.fromJson(json));
}
- effect 网络请求处理
Effect buildEffect() {
return combineEffects(
- reducer 刷新state
Reducer buildReducer() {
return asReducer(
扩展一下代码中使用的webview控件
flutter_webview_plugin
使用方法示例
class WebPage extends StatefulWidget {
final String webUrl;
final String webTitle;
WebPage({this.webUrl, this.webTitle});
@override
_WebPageState createState() => _WebPageState();
}
class _WebPageState extends State {
@override
Widget build(BuildContext context) {
return WebviewScaffold(
url: widget.webUrl,
appBar: AppBar(
title: Text(widget.webTitle),
),
);
}
}
- 例子只是展示web作用,传入对应url即可