Flutter JSON字符串和Map类型的转换
Map转换成Json
import 'dart:convert'
Map userInfo={
"username":"张三",
"age":20
};
print(json.encode(userInfo));
Json转换成Map
String str='{"username":"张三","age":20}';
print(json.decode(str));
Map info=json.decode(str);
print(info["username"]);
Flutter Dio请求数据
dio是一个强大的Dart Http请求库,支持Restful API、FormData、拦截器、请求取消、Cookie管理、文件上传/下载、超时、自定义适配器等...
官网:
中文文档:
https://github.com/flutterchina/dio/blob/master/README-ZH.md
Dio的配置
dependencies:
dio: ^4.0.6
import 'package:dio/dio.dart';
var response = await Dio().get('https://jdmall.itying.com/api/httpGet');
print(response.data);
print(response.data is Map);
}
Dio Get
_getData() async {
var response = await
Dio().get('https://jdmall.itying.com/api/httpGet',queryParameters:{
"username":"zhangsan"
});
print(response.data is Map);
print(response.data["msg"]);
}
Dio Post
_postData() async{
var response = await
Dio().post('https://jdmall.itying.com/api/httpPost',data: {"username":"张三
111","age":"20"});
print(response.data);
print(response.data is Map);
}
Dio Put
_putData() async {
var response = await Dio().put('https://jdmall.itying.com/api/httpPut',
data: {"username": "张三111", "age": "20"});
print(response.data);
print(response.data is Map);
}
Dio Delete
_deleteData() async {
var response = await Dio().delete(
'https://jdmall.itying.com/api/httpDelete',
queryParameters: {"id": "1"});
print(response.data);
print(response.data is Map);
}
Dio 请求数据渲染数据示例,通过数组长度判断数据渲染数据
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List _list = [];
_getData() async {
var response = await Dio().get('https://jdmall.itying.com/api/pcate');
// print(response);
setState(() {
_list = response.data['result'];
});
}
@override
void initState() {
super.initState();
_getData();
}
@override
Widget build(BuildContext context) {
return _list.isNotEmpty ? ListView(
children: _list.map((e) {
return Column(
children: [
ListTile(
title: Text(e['title']),
),
const Divider(),
],
);
}).toList()
) : const Center(
child: CircularProgressIndicator(),
);
}
}
Dio 请求数据渲染数据示例,FutureBuilder渲染数据
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
_getData() async {
var response = await Dio().get('https://jdmall.itying.com/api/pcate');
// print(response);
return response.data["result"];
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _getData(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Center(child: Text("Error:${snapshot.error}"));
} else {
var response = snapshot.data as List;
return ListView(
children: response.map((value) {
return ListTile(
title: Text("${value["title"]}"),
);
}).toList(),
);
}
} else {
return const Center(child: CircularProgressIndicator());
}
});
;
}
}