简体   繁体   English

Flutter - 无法从REST API获取数据

[英]Flutter - Unable to get data from REST API

I am trying to load data from my REST API and load them into the UI. 我正在尝试从REST API加载数据并将其加载到UI中。 Below is my code 以下是我的代码

 FutureBuilder loadProductSellAds() {
    return FutureBuilder<ProductSellAdvertisement>(
      future: DataFetch().loadProductSellSingleAd(
          AppNavigation.getAPIUrl() +
              "sellAdvertisement/getAdByID/" +
              widget._advertisementID.toString()),
      builder: (context, snapshot) {
        if (snapshot.hasError) print(snapshot.error);

        if(snapshot.hasData)
        {
          ProductSellAdvertisement p = snapshot.data;

          setValuesToProductDataSection(
            type: p.type,
            location: p.location,
            quantity: p.quantity.toString(),
            stocksAvailable: p.stocksAvailableTill.toString(),
            unitPrice: p.unitPrice.toString()

          );
          displayProductInfoSection = true;
        }
      },
    );
  }

datafetch.dart datafetch.dart

/**
   * Use to Load Product Sell Advertisements
   * 
   **/
  Future<ProductSellAdvertisement> loadProductSellSingleAd(String url) async {
    final response = await http.get(url);
    // Use the compute function to run parsePhotos in a separate isolate
    final parsed =
        convert.json.decode(response.body).cast<Map<String, dynamic>>();

    ProductSellAdvertisement obj = parsed
        .map<ProductSellAdvertisement>(
            (json) => new ProductSellAdvertisement.fromJson(json));
    return obj;
  }

and finally, the model class, product_sell_advertisement.dart 最后,模型类, product_sell_advertisement.dart

import './product.dart';
import './product_unit.dart';
import './user.dart';

import 'package:json_annotation/json_annotation.dart';


part 'product_sell_advertisement.g.dart';

@JsonSerializable()

class ProductSellAdvertisement
{
     int idproductSellAdvertisement;
     Product product;
     ProductUnit productUnits;
     User user;
     String type;
     String grade;
     double unitPrice;
     double quantity;
     String location;
     int stocksAvailableTill;
     String extraInformation;
     int expireOn;
     int deleteTimestamp;
     int dateCreated;
     int lastUpdated;

     ProductSellAdvertisement(this.idproductSellAdvertisement, this.product, this.productUnits, this.user, this.type,
     this.grade, this.unitPrice, this.quantity, this.location, this.stocksAvailableTill, this.extraInformation, this.expireOn,
     this.deleteTimestamp,this.dateCreated, this.lastUpdated);


  factory ProductSellAdvertisement.fromJson(Map<String, dynamic> json) => _$ProductSellAdvertisementFromJson(json);


  Map<String, dynamic> toJson() => _$ProductSellAdvertisementToJson(this);
}

When I execute the loadProductSellAds method (the first code in this post), I run into the below error 当我执行loadProductSellAds方法(这篇文章中的第一个代码)时,我遇到了以下错误

    [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments.
E/flutter ( 4670): Receiver: _LinkedHashMap len:15
E/flutter ( 4670): Tried calling: cast<Map<String, dynamic>>()
E/flutter ( 4670): Found: cast<RK, RV>() => Map<RK, RV>
E/flutter ( 4670): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
E/flutter ( 4670): #1      DataFetch.loadProductSellSingleAd 
package:xxx/custom_functions/data_fetch.dart:101
E/flutter ( 4670): <asynchronous suspension>
E/flutter ( 4670): #2      SingleSellAdvertisementState.loadProductSellAds 
package:xxx/pages/single_sell_advertisement_page.dart:225
E/flutter ( 4670): #3      SingleSellAdvertisementState.initState 
package:xxx/pages/single_sell_advertisement_page.dart:5

Why is this happening? 为什么会这样?

By definition that cast() is a method introduced in Dart 2 on Iterable that actually creates a new iterable of type Iterable (or subclass List) filled with the values of the original interable. 根据定义,cast()是Dart 2中在Iterable上引入的一种方法,它实际上创建了一个新的迭代类型Iterable(或子类List),其中填充了原始interable的值。 Your source object is not an instance if Iterable. 如果Iterable,您的源对象不是实例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM