簡體   English   中英

“null”類型不是“map string dynamic”類型的子類型 flutter

[英]type 'null' is not a subtype of type 'map string dynamic ' flutter

我正在嘗試在我的應用程序中使用 API 它可以正常工作,但是當 JSON 獲取 null 時,該應用程序無法正常工作。

正常工作:

{
   "product":{
      "description_1":"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
      "description_2":"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.",
      "description_3":"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteratenter code hereion in some form",
      "description_4":"The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested.",
      "description_5":"Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like"
   }
}

錯誤:“類型‘null’不是‘映射字符串動態’類型的子類型”:

{ “產品”:null }

Class:

class Store {
  Store({
    required this.product,
  });

  Product product;

  factory Store.fromJson(Map<String, dynamic> json) => Store(
        product: Product.fromJson(json["product"]),
      );

  Map<String, dynamic> toJson() => {
        "product": product.toJson(),
      };
}

class Product {
  Product({
    required this.description1,
    required this.description2,
    required this.description3,
  });

  String description1;
  String description2;
  String description3;

  factory Product.fromJson(Map<String, dynamic> json) => Product(
        description1: json["description_1"] ?? "",
        description2: json["description_2"] ?? "",
        description3: json["description_3"] ?? "",
      );

  Map<String, dynamic> toJson() => {
        "description_1": description1,
        "description_2": description2,
        "description_3": description3,
      };
}

如果產品實際上可以是null那么Store需要處理它。 Store class product屬性更改為可以為空? 像下面這樣的符號:

class Store {
  Store({
    this.product,
  });

  Product? product;

  factory Store.fromJson(Map<String, dynamic> json) => Store(
        product: json["product"] != null 
            ? Product.fromJson(json["product"])
            : null,
      );

  Map<String, dynamic> toJson() => {
        "product": product?.toJson(),
      };
}

輸入'列表<dynamic> ' 不是類型 'Map 的子類型<string, dynamic in flutter app< div><div id="text_translate"><p> 我正在嘗試在 Flutter 中創建一個 API 請求,但我收到以下錯誤作為響應</p><blockquote><p> 'List&lt;dynamic&gt;' 類型不是 'Map&lt;String, dynamic&gt;' 類型的子類型</p></blockquote><p>我正在嘗試創建第一個 API 並請讓我知道該方法是否可行</p><p>這是我的代碼</p><pre>import 'package:flutter/material.dart'; class Product { final int id; final String title, description; final String images; final List&lt;Color&gt; colors; final double price; final double rating; final bool isFavourite, isPopular; Product( {this.id, this.images, this.colors, this.title, this.price, this.rating, this.description, this.isFavourite, this.isPopular}); factory Product.fromJson(Map&lt;String, dynamic&gt; json) { return Product( id: json['id'], images: json['images'], title: json['title'], price: json['price'], rating: json['rating'], description: json['description'], ); } } Future&lt;Product&gt; fetchProd() async { final response = await http.get('https://test.com/sampleapi.php'); if (response.statusCode == 200) { // If the server did return a 200 OK response, // then parse the JSON. return Product.fromJson(jsonDecode(response.body)); } else { // If the server did not return a 200 OK response, // then throw an exception. throw Exception('Failed to load album'); } } class ProdList extends StatefulWidget { @override _ProdListState createState() =&gt; _ProdListState(); } class _ProdListState extends State&lt;ProdList&gt; { Future&lt;Product&gt; futureProdLists; @override void initState() { super.initState(); futureProdLists = fetchProd(); } @override Widget build(BuildContext context) { return Column(children: [ Padding( padding: EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)), child: SectionTitle(title: "Popular Products", press: () {}), ), SizedBox(height: getProportionateScreenWidth(20)), SingleChildScrollView( scrollDirection: Axis.horizontal, child: FutureBuilder&lt;Product&gt;( future: futureProdLists, builder: (context, snapshot) { print(snapshot); if (snapshot.hasData) { return Text(snapshot.data.title); } else if (snapshot.hasError) { return Text("${snapshot.error}"); }</pre><p> 這是我的示例 API</p><pre> [ { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true }, { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true }, { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true }, { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true }, { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true } ]</pre></div></string,></dynamic>

[英]type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic in flutter app

暫無
暫無

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

相關問題 “Null”類型不是“Map”類型的子類型<string, dynamic> ' flutter 錯誤</string,> “Null”類型不是“Map”類型的子類型<string, dynamic> '</string,> Flutter - 列表不是 Map 類型的子類型<string, dynamic></string,> Flutter StreamBuilder 錯誤類型“Null”不是“Map”類型的子類型<string, dynamic> ' 在類型轉換中</string,> Flutter:_CastError(類型“Null”不是“Map”類型的子類型<string, dynamic> ' 在類型轉換中)</string,> _TypeError(輸入&#39;列表<dynamic> &#39; 不是類型 &#39;Map 的子類型<String, dynamic> &#39;) 在顫動 輸入&#39;列表<dynamic> &#39; 不是類型 &#39;Map 的子類型<String, dynamic> &#39;顫動 顫振:輸入&#39;列表<dynamic> &#39; 不是類型 &#39;Map 的子類型<String, dynamic> &#39; 輸入'列表<dynamic> ' 不是類型 'Map 的子類型<string, dynamic in flutter app< div><div id="text_translate"><p> 我正在嘗試在 Flutter 中創建一個 API 請求,但我收到以下錯誤作為響應</p><blockquote><p> 'List&lt;dynamic&gt;' 類型不是 'Map&lt;String, dynamic&gt;' 類型的子類型</p></blockquote><p>我正在嘗試創建第一個 API 並請讓我知道該方法是否可行</p><p>這是我的代碼</p><pre>import 'package:flutter/material.dart'; class Product { final int id; final String title, description; final String images; final List&lt;Color&gt; colors; final double price; final double rating; final bool isFavourite, isPopular; Product( {this.id, this.images, this.colors, this.title, this.price, this.rating, this.description, this.isFavourite, this.isPopular}); factory Product.fromJson(Map&lt;String, dynamic&gt; json) { return Product( id: json['id'], images: json['images'], title: json['title'], price: json['price'], rating: json['rating'], description: json['description'], ); } } Future&lt;Product&gt; fetchProd() async { final response = await http.get('https://test.com/sampleapi.php'); if (response.statusCode == 200) { // If the server did return a 200 OK response, // then parse the JSON. return Product.fromJson(jsonDecode(response.body)); } else { // If the server did not return a 200 OK response, // then throw an exception. throw Exception('Failed to load album'); } } class ProdList extends StatefulWidget { @override _ProdListState createState() =&gt; _ProdListState(); } class _ProdListState extends State&lt;ProdList&gt; { Future&lt;Product&gt; futureProdLists; @override void initState() { super.initState(); futureProdLists = fetchProd(); } @override Widget build(BuildContext context) { return Column(children: [ Padding( padding: EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)), child: SectionTitle(title: "Popular Products", press: () {}), ), SizedBox(height: getProportionateScreenWidth(20)), SingleChildScrollView( scrollDirection: Axis.horizontal, child: FutureBuilder&lt;Product&gt;( future: futureProdLists, builder: (context, snapshot) { print(snapshot); if (snapshot.hasData) { return Text(snapshot.data.title); } else if (snapshot.hasError) { return Text("${snapshot.error}"); }</pre><p> 這是我的示例 API</p><pre> [ { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true }, { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true }, { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true }, { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true }, { "id": "0001", "images": "assets/images/ps4_console_white_1.png", "title": "Wireless Controller for PS4", "price": 25, "description": "description", "rating": 4.8, "rating (copy)": 4.8, "isFavourite": true, "isPopular": true } ]</pre></div></string,></dynamic> Flutter _TypeError(類型為“列表” <dynamic> &#39;不是&#39;Map類型的子類型 <String, dynamic> &#39;)
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM