添加依赖

  1. dio: ^4.0.3

创建工具类

  1. import 'package:dio/dio.dart';
  2. class NetCore {
  3. static final NetCore _instance = NetCore.internal();
  4. late Dio _dio;
  5. factory NetCore() {
  6. return _instance;
  7. }
  8. NetCore.internal() {
  9. initNetConfig();
  10. }
  11. void initNetConfig() {
  12. //默认配置
  13. var options = BaseOptions(
  14. baseUrl: "",
  15. connectTimeout: 5000,
  16. receiveTimeout: 3000,
  17. );
  18. _dio = Dio(options);
  19. }
  20. Future<Response> get(String url) async {
  21. Response response = await _dio.get(url);
  22. return response;
  23. }
  24. }