Search code examples
apiprestashop

Cant retrieve data from Prestashop API and display on flutter


I am trying to retrieve data from Prestashop and get it on flutter widget. this is the code: but I got error as is is also below.

What I need to know is to start getting prestashop data and display it to my flutter app such Products - Orders - customers I created Class where get Future data from prestashop and action will be given by flat button which should return a json. The Link is correct and also the Webservice key in prestashop

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(HomePage());
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  Future getData() async {
    http.Response response = await http
        .get('https://uibox.store/api/products/1?output_format=JSON', headers: {
      "Autorization": ('74R9CQC6SX6Y2P369DLZ73VFB9AB5LV1'),
      "Accept": "application/json",
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.blue,
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: Colors.black,
          title: Text('TestAPI'),
        ),
        body: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Center(
                child: FlatButton(
                    color: Colors.white,
                    onPressed: () {
                      getData();
                    },
                    child: Text('Click here to get data')),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

This is the error I get

Error: XMLHttpRequest error.
    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:20      get current
packages/http/src/browser_client.dart 84:22                                       <fn>
dart-sdk/lib/async/zone.dart 1450:54                                              runUnary
dart-sdk/lib/async/future_impl.dart 143:18                                        handleValue
dart-sdk/lib/async/future_impl.dart 696:44                                        handleValueCallback
dart-sdk/lib/async/future_impl.dart 725:32                                        _propagateToListeners
dart-sdk/lib/async/future_impl.dart 519:7                                         [_complete]
dart-sdk/lib/async/stream_pipe.dart 61:11                                         _cancelAndValue
dart-sdk/lib/async/stream.dart 1229:7                                             <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39  dcall
dart-sdk/lib/html/dart2js/html_dart2js.dart 37204:58                              <fn>


    at Object.createErrorWithStack (http://localhost:34359/dart_sdk.js:4477:12)
    at Object._rethrow (http://localhost:34359/dart_sdk.js:37464:16)
    at async._AsyncCallbackEntry.new.callback (http://localhost:34359/dart_sdk.js:37458:13)
    at Object._microtaskLoop (http://localhost:34359/dart_sdk.js:37290:13)
    at _startMicrotaskLoop (http://localhost:34359/dart_sdk.js:37296:13)
    at http://localhost:34359/dart_sdk.js:32918:9

Solution

  • you need to encode your key authorization like this:

    String username = 'YOUR-KEY';
    String password = ':';
    String auth =
        'Basic ' + base64Encode(utf8.encode('$username:$password'));
    
     
    Response response = await get('https://uibox.store/api/products/1?output_format=JSON',
        headers: <String, String>{'authorization': auth,});
    

    check out this answer