1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter: Api Request is taking too long to process

Discussão em 'Mobile' iniciado por Stack, Setembro 28, 2024 às 11:22.

  1. Stack

    Stack Membro Participativo

    I'm trying to make an API request in my flutter application but it takes too much time to process like 15-25 seconds.When i run the same request on Postman its working fine and takes only 200 to 650 ms.I tried using diohttp and http client but result is almost same.Below is my code.

    import 'package:http/http.dart' as http;
    import 'package:dio_http/dio_http.dart';

    void main() {
    WidgetsFlutterBinding.ensureInitialized();
    runApp(MyApp());
    }

    class MyApp extends StatelessWidget {
    MyApp({super.key});

    Dio dioRest = Dio(
    BaseOptions(
    baseUrl: baseURL,
    headers: {
    HttpHeaders.contentTypeHeader: ContentType.json.value,
    },
    ),
    );

    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    title: 'MYAPP',
    home: Scaffold(
    appBar: AppBar(title: Text("Home page")),
    body: Center(
    child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: [
    MaterialButton(
    child: Text("Go to my profile"),
    color: Theme.of(context).primaryColor,
    onPressed: () async{
    await getAllContentList();
    }),
    MaterialButton(
    child: Text("Go to Accounts"),
    color: Theme.of(context).primaryColor,
    onPressed: () async{
    await getAllContentListHttp();
    }),
    ],
    ),
    ),
    ),
    );

    }

    Future<List<dynamic>> getAllContentList() async {
    try {
    print('Api Started');
    var result = await dioRest.post(baseURL+'secured/v/getAllContentList', options: Options(
    headers: {
    'Authorization': 'bearer $token',
    'Content-Type': 'application/json',
    },
    ));
    print('API result :: $result');
    if (result.statusCode == 200) {
    print('API result decoded :: ${jsonDecode(result.data)}');
    return jsonDecode(result.data);
    }
    throw DioError(requestOptions: result.requestOptions);
    } on DioError catch (error) {
    if (error.response!.statusCode! >= 400) {
    throw AccessTokenException(message: "Token invalid or expired");
    }
    var businessError = BusinessError.fromJson(error.response?.data);
    throw BusinessException(businessError, statusCode: error.response?.statusCode);
    } catch (error) {
    throw Error();
    }
    }

    Future<List<dynamic>> getAllContentListHttp() async {
    try {
    print('Api Started');
    Map<String, String> requestHeaders = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer $token',
    };
    String stUrl = baseURL+'protected/users/getAllActiveStreamList';
    final response = await http.post(Uri.parse(baseURL+'secured/v/getAllContentList'), headers: requestHeaders);
    print('API result :: $response');
    if (response.statusCode == 200) {
    print('API result decoded :: ${jsonDecode(response.body)}');
    return jsonDecode(response.body);
    }
    throw AccessTokenException(message: "!!!Sorry");
    } on DioError catch (error) {
    if (error.response!.statusCode! >= 400) {
    throw AccessTokenException(message: "Token invalid or expired");
    }
    var businessError = BusinessError.fromJson(error.response?.data);
    throw BusinessException(businessError, statusCode: error.response?.statusCode);
    } catch (error) {
    throw Error();
    }
    }
    }


    Versions used :

    http: ^0.13.5
    dio_http: ^5.0.4

    Continue reading...

Compartilhe esta Página