addproj_serv.dart 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // ignore_for_file: use_build_context_synchronously
  2. import 'dart:convert';
  3. import 'package:dio/dio.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:go_router/go_router.dart';
  6. import '../globals.dart';
  7. Future<void> addproject(BuildContext context, String name, String description /*Map data*/) async {
  8. final dio = Dio();
  9. Response response;
  10. AlertDialog alert2 = AlertDialog(
  11. title: Text("Add Project Gagal"),
  12. content: Text("Data Invalid"),
  13. actions: [
  14. TextButton(
  15. child: Text('Ok'),
  16. onPressed: () => Navigator.of(context).pop(),
  17. ),
  18. ],
  19. );
  20. var auth = 'Basic ${base64Encode(utf8.encode('$USER_LOGIN:$PASS_LOGIN'))}';
  21. Map<String, String> headers = {
  22. 'content-type': 'application/json',
  23. 'accept': 'application/json',
  24. 'authorization': auth
  25. };
  26. try{
  27. response = await dio.post("http://localhost:8080/api/v1/projects",
  28. options: Options(headers: headers),
  29. data:
  30. // data
  31. {'name':name,'description':description}
  32. );
  33. if (response.statusCode == 201){
  34. return context.go('/listproject');
  35. }
  36. } catch (error){
  37. showDialog(context: context, builder: (context) => alert2);
  38. }
  39. }