簡體   English   中英

Flutter - 無法從REST API獲取數據

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

我正在嘗試從REST API加載數據並將其加載到UI中。 以下是我的代碼

 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

/**
   * 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;
  }

最后,模型類, 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);
}

當我執行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

為什么會這樣?

根據定義,cast()是Dart 2中在Iterable上引入的一種方法,它實際上創建了一個新的迭代類型Iterable(或子類List),其中填充了原始interable的值。 如果Iterable,您的源對象不是實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM