【发布时间】:2019-09-22 16:27:23
【问题描述】:
这是我的产品供应商:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import '../models/Product.dart';
class Products with ChangeNotifier {
List<Product> _items = [];
List<Product> get items {
return [..._items];
}
Future<void> getProducts() async {
try {
final response =
await Dio().get("https://jsonplaceholder.typicode.com/posts");
final List<Product> body = response.data;
_items = body;
notifyListeners();
} on DioError catch (e) {
print(e);
}
}
}
然后,这是我的产品模型:
class Product {
final String id;
final String title;
final String body;
final String userId;
Product({this.id, this.title, this.body, this.userId});
factory Product.fromJson(Map<String, dynamic> json) {
return Product(
id: json['id'],
title: json['title'],
body: json['body'],
userId: json['userId'],
);
}
}
但是,在getProducts() 函数中,如果我将_items 分配给response.data,它会说
“列表”不是“列表”类型的子类型。
我在这里做错了吗?
【问题讨论】: