无法从Prestashop API检索数据并在颤动下显示

时间:2020-06-18 08:33:38

标签: api prestashop

我正在尝试从Prestashop检索数据并将其放在flutter小部件上。 这是代码: 但是我也收到下面的错误。

我需要知道的是开始获取prestashop数据并将其显示在我的flutter应用程序中,例如 产品-订单-客户 我创建了一个类,其中从prestashop获取Future数据,动作将由平面按钮给出,该按钮应返回json。 Link是正确的,也是prestashop中的Web服务密钥

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')),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

这是我得到的错误

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

2 个答案:

答案 0 :(得分:2)

您需要像这样对密钥授权进行编码:

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

签出此answer

答案 1 :(得分:0)

您必须对认证密钥进行base64编码。

尝试一下:(我不是一个笨拙的程序员)

import 'dart:convert';
Codec<String, String> stringToBase64 = utf8.fuse(base64);
String encoded = stringToBase64.encode(credentials);

More info.