简体   繁体   English

I am trying to fetch collection from shopify admin api with rest api in flutter

[英]I am trying to fetch collection from shopify admin api with rest api in flutter

I am Using flutter with http package.我正在使用 flutter 和 http package。 in my code CircularProgressIndicator() is running and is not showing any error here is my http request code在我的代码中CircularProgressIndicator()正在运行并且没有显示任何错误这里是我的 http 请求代码

import 'package:http/http.dart' as http;
import 'dart:convert';`

class Collections {
  Future<List> getAllCollections() async {
    var headers = {
      'Content-Type': 'application/json',
      'X-Shopify-Access-Token': '{token}',
    };
    var url = Uri.parse(
        'https://{shop}.myshopify.com/admin/api/2022-07/collections.json');
    try {
      var res = await http.get(url, headers: headers);
      print(res);
      if (res.statusCode == 200) {
        return jsonDecode(res.body);
      } else {
        return Future.error("Server Error");
      }
    } catch (SocketException) {
      return Future.error("Error Fetching Data");
    }
  }
}

and here I created the object of this class and using it.在这里我创建了这个 class 的 object 并使用它。

class ProCategories extends StatefulWidget {
  const ProCategories({Key? key}) : super(key: key);

  @override
  State<ProCategories> createState() => _ProCategoriesState();
}

class _ProCategoriesState extends State<ProCategories> {
  Collections collections = Collections();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Categories"),
        centerTitle: true,
        foregroundColor: Colors.black,
        backgroundColor: Colors.white,
      ),
      body: Container(
        margin: EdgeInsets.only(top: 8.0),
        child: FutureBuilder<List>(
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              if (snapshot.data?.length == 0) {
                return Center(
                  child: Text("0 Collections"),
                );
              }
              return ListView.builder(
                  itemCount: snapshot.data?.length,
                  itemBuilder: (context, index) {
                    return Card(
                      child: ListTile(
                        title: Row(
                          children: [
                            Expanded(
                              child: Container(
                                child: Text(
                                  snapshot.data![index]['collections']['edges']
                                      ['node']['title'],
                                  style: TextStyle(
                                      fontSize: 25,
                                      fontWeight: FontWeight.bold),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    );
                  });
            } else if (snapshot.hasError) {
              return Center(
                child: Text(snapshot.error.toString()),
              );
            } else {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
          },
        ),
      ),
    );
  }
}

Add future to FutureBuilder .未来添加到FutureBuilder

late Future<List> getAllCollections;

@override
void initState() {
  super.initState();
  getAllCollections = collections.getAllCollections();
}

@override
Widget build(BuildContext context) {
  return Scaffold(
...
    body: Container(
      margin: EdgeInsets.only(top: 8.0),
      child: FutureBuilder<List>(
        future: getAllCollections,
        builder: (context, snapshot) {
...
        },
      ),
    ),
  );
}

See Fetch data from the internet for the full example.有关完整示例,请参阅从 Internet 获取数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 REST API 获取数据并显示在 Listview 上,Flutter - How to fetch data from REST API and display on Listview, Flutter 登录后如何从 rest ZDB974238714CA8DE634A7CE1D08 - How can I fetch the Flutter Provider state from an rest API once after login? 我正在尝试从 api 获取数据并将其显示在 DropDownButton 中。 问题是快照返回 null - I am trying to fetch data from the api and display it in a DropDownButton. problem is that snapshot is returning null 我无法在 flutter 中对特定 url 进行 REST api 调用 - I am unable to make REST api calls on a specific url in flutter 我正在尝试从 api 获取数据并在列表或卡片视图中显示 - I am trying to fetch data from api and show in list or card view 我正在尝试从 API 中获取 TimeZones,但由于某种原因,它没有返回所有 timeZones - I am trying to get TimeZones from an API in flutter but due to some reason its not returning all timeZones 我正在尝试使用 https.MultipartRequest 在 Flutter 中调用 API - I am trying to make a call to an API with https.MultipartRequest in Flutter Flutter 使用 Django Rest Api 来获取数据 - Flutter WIth Django Rest Api to fetch data 我正在尝试借助颤振中的模型类从 api 加载数据 - I am trying to load data from api with the help of model class in flutter Flutter 从 Api 获取数据 - Flutter fetch Data from Api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM