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();
    });
  }
}

三,效果

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

相关推荐
晚霞的不甘11 小时前
Flutter/OpenHarmony 三方库适配实战:google_maps_flutter 地图
flutter·macos·华为·cocoa·harmonyos·1024程序员节
恋猫de小郭16 小时前
Flutter HC 旋转 ANR,而且还和之前的 HCPP 问题类似
android·前端·flutter
恋猫de小郭2 天前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
程序员老刘2 天前
Flutter版本选择指南:尽早升级3.47.9 | 2026年11月
flutter·ai编程·客户端
恋猫de小郭3 天前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
恋猫de小郭4 天前
Flutter A2UI 1.0 ,原来的 genui 也正式换成了这一套 SDK 了
android·前端·flutter
LinXunFeng4 天前
A2UI 1.0 详解:让 AI 生成 Flutter 界面,还不怕它乱写
前端·flutter·agent
好的佩奇4 天前
Dart 之元数据与注解
android·flutter·dart
恋猫de小郭7 天前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
好的佩奇7 天前
蓝牙之设备角色与连接
android·flutter·蓝牙