flutter Dio发送post请求

一,导包

python 复制代码
dependencies:
  flutter:
    sdk: flutter
  dio: ^4.0.6

二,请求、解析、刷新渲染

Dart 复制代码
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

main() {
  runApp(const MaterialApp(
    home: H(),
  ));
}

class H extends StatefulWidget {
  const H({super.key});

  @override
  State<H> createState() => _HState();
}

class _HState extends State<H> {
  String _resStr = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("抓包测试"),
        ),
        body: Center(
          child: Text(
            _resStr,
            style: const TextStyle(fontSize: 30),
          ),
        ),
        floatingActionButton:
            FloatingActionButton(onPressed: _postRequestFunction));
  }

  void _postRequestFunction() async {
    Dio dio = Dio();
    FormData formData = FormData.fromMap({"sourceText": "Who are you?"});
    String url = "https://api.oioweb.cn/api/txt/QQFanyi";
    Response response = await dio.post(url, data: formData);
    setState(() {
      _resStr =
          json.decode(response.toString())["result"]["targetText"].toString();
    });
  }
}

三,效果

点击按钮,屏幕中心出现对应的翻译结果

相关推荐
神经蛙397119 小时前
settings.gradle' line: 22 * What went wrong: Plugin [id: 'org.jetbrains.kotlin.a
flutter
stringwu20 小时前
一个bug 引发的Dart 与 Java WeakReference 对比探讨
flutter
火柴就是我2 天前
从头写一个自己的app
android·前端·flutter
●VON2 天前
Flutter 项目成功运行后,如何正确迁移到 OpenHarmony?常见疑问与跳转失效问题解析
flutter·华为·openharmony·开源鸿蒙
●VON2 天前
Flutter 编译开发 OpenHarmony 全流程实战教程(基于 GitCode 社区项目)
flutter·openharmony·gitcode
消失的旧时光-19432 天前
Flutter 组件:Row / Column
flutter
程序员老刘3 天前
Flutter版本选择指南:3.35稳定,3.38发布 | 2025年11月
flutter·客户端
kirk_wang3 天前
Flutter 3.38和Dart 3.10中最大的更新
flutter
前端小伙计3 天前
Flutter 配置国内镜像,加速项目加载!
flutter
zonda的地盘3 天前
开发 Flutter Plugin 之 初始配置
flutter