1、新建项目

image.png

2、编写Server端

1、创建一个文件夹,然后新建main.dart

image.png

2、按下F5后会出现

image.png
修改里面的内容
image.png

3、使用Dart编写一个服务处理

  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'data.dart';
  4. main() async{
  5. var requestServer = await HttpServer.bind("192.168.51.211", 8080);
  6. print("http服务启动起来了");
  7. //处理请求
  8. await for(HttpRequest request in requestServer){
  9. // request.response..write('request success...')..close();//..是语法糖,表示可以一直请求,不用再写
  10. handleMessage(request);
  11. }
  12. }
  13. void handleMessage(HttpRequest request){
  14. try{
  15. if(request.method == "GET"){
  16. handleGET(request);
  17. }else if(request.method == "POST"){
  18. //TODO
  19. handleGET(request);
  20. }
  21. }catch(e){
  22. print("捕获了一个异常:$e");
  23. }
  24. }
  25. void handleGET(HttpRequest request){
  26. //获取请求参数
  27. var action = request.uri.queryParameters['action'];
  28. if(action == 'getProducts'){
  29. print("获取产品数据...");
  30. request.response..statusCode = HttpStatus.ok
  31. ..write(json.encode(products))..close();
  32. }
  33. if(action == 'getNews'){
  34. print("获取新闻数据...");
  35. request.response..statusCode = HttpStatus.ok
  36. ..write(json.encode(news))..close();
  37. }
  38. }
  39. void handlePOST(HttpRequest request){
  40. //处理POST请求
  41. }

4、编写data.dart

  1. //产品数据
  2. var products = {
  3. "items":[
  4. {
  5. "desc": "屏幕尺寸: 13.3英寸 处理器: Intel Core i5-8259",
  6. "imageUrl":"assets/images/products/1.jpeg",
  7. "type":"新款",
  8. "name":"苹果笔记本",
  9. },
  10. {
  11. "desc": "处理器:Intel Core i7-5960X 内存大小:32GB 显卡芯片:双NVIDIA GeForce GTX ",
  12. "imageUrl":"assets/images/products/2.jpeg",
  13. "type": "新款",
  14. "point":"强悍",
  15. "name":"外星人笔记本",
  16. },
  17. {
  18. "desc": "Intel,至强处理器E5系列 英特尔C610系列芯片组 机械硬盘:SAS,SATA,近线SAS;固态硬盘:SAS,SATA",
  19. "imageUrl":"assets/images/products/3.jpeg",
  20. "type": "经典",
  21. "point":"稳定性好噪音小",
  22. "name":"戴尔 PowerEdge R730",
  23. },
  24. {
  25. "desc": "屏幕尺寸: 13.3英寸 处理器: Intel Core i5-8259",
  26. "imageUrl":"assets/images/products/1.jpeg",
  27. "type": "新款",
  28. "point":"超轻超薄",
  29. "name":"苹果笔记本",
  30. },
  31. {
  32. "desc": "屏幕尺寸: 13.3英寸 处理器: Intel Core i5-8259",
  33. "imageUrl":"assets/images/products/1.jpeg",
  34. "type": "新款",
  35. "point":"超轻超薄",
  36. "name":"苹果笔记本",
  37. },
  38. {
  39. "desc": "处理器:Intel Core i7-5960X 内存大小:32GB 显卡芯片:双NVIDIA GeForce GTX ",
  40. "imageUrl":"assets/images/products/2.jpeg",
  41. "type": "新款",
  42. "point":"强悍",
  43. "name":"外星人笔记本",
  44. },
  45. {
  46. "desc": "Intel,至强处理器E5系列 英特尔C610系列芯片组 机械硬盘:SAS,SATA,近线SAS;固态硬盘:SAS,SATA",
  47. "imageUrl":"assets/images/products/3.jpeg",
  48. "type": "经典",
  49. "point":"稳定性好噪音小",
  50. "name":"戴尔 PowerEdge R730",
  51. },
  52. {
  53. "desc": "屏幕尺寸: 13.3英寸 处理器: Intel Core i5-8259",
  54. "imageUrl":"assets/images/products/1.jpeg",
  55. "type": "新款",
  56. "point":"超轻超薄",
  57. "name":"苹果笔记本",
  58. },
  59. ]
  60. };
  61. var news = {
  62. "items":[
  63. {
  64. "author":"张小平",
  65. "title":"添加商品",
  66. "content":"小张添加了一个关于服务器的商品"
  67. },
  68. {
  69. "author": "陈软发",
  70. "title":"好消息",
  71. "content": "公司服务器全年成功销售1万台",
  72. },
  73. {
  74. "author": "李东升",
  75. "title":"收集商品信息",
  76. "content": "销售人员开始找每个商家收集商品信息,包括商品名称,价格,活动等",
  77. },
  78. ]
  79. };

测试请求

  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:http/http.dart' as http;
  4. void main() {
  5. runApp(MyApp());
  6. }
  7. class MyApp extends StatelessWidget {
  8. // This widget is the root of your application.
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: 'Flutter Demo',
  13. theme: ThemeData(
  14. primarySwatch: Colors.blue,
  15. visualDensity: VisualDensity.adaptivePlatformDensity,
  16. ),
  17. home: MyHomePage(title: 'Flutter Demo Home Page'),
  18. );
  19. }
  20. }
  21. class MyHomePage extends StatefulWidget {
  22. MyHomePage({Key key, this.title}) : super(key: key);
  23. final String title;
  24. @override
  25. _MyHomePageState createState() => _MyHomePageState();
  26. }
  27. class _MyHomePageState extends State<MyHomePage> {
  28. @override
  29. Widget build(BuildContext context) {
  30. return Scaffold(
  31. appBar: AppBar(
  32. title: Text(widget.title),
  33. ),
  34. body: Center(
  35. child: RaisedButton(
  36. child: Text("按钮"),
  37. onPressed: () async {
  38. String url = "http://192.168.51.211:8080/?action=getProducts";
  39. var res = await http.get(url);
  40. var body = res.body;
  41. print("body = $body");
  42. var json = jsonDecode(body);
  43. },
  44. ),
  45. ),
  46. );
  47. }
  48. }