Flutter - type '_InternalLinkedHashMap<String, dynamic> 不是 List<dynamic> 类型的子类型

Posted

技术标签:

【中文标题】Flutter - type \'_InternalLinkedHashMap<String, dynamic> 不是 List<dynamic> 类型的子类型【英文标题】:Flutter - type '_InternalLinkedHashMap<String, dynamic> is not a subtype of type List<dynamic>Flutter - type '_InternalLinkedHashMap<String, dynamic> 不是 List<dynamic> 类型的子类型 【发布时间】:2021-01-22 02:14:10 【问题描述】:

你好我创建了一个新的flutter应用程序,我的数据库是mysql数据库,我的Json数据是一个数组,我已经尽力做到最好但总是弹出这个错误:

 type '_InternalLinkedHashMap<String, dynamic> is not a subtype of type List<dynamic>

Dataclass.dart

class FolderList 
  final List<Data> data;

  FolderList(this.data);

  factory FolderList.fromJson(Map<String, dynamic> json) 
    return FolderList(
      data: json['folders'] != null ? (json['folders'] as List).map((i) => Data.fromJson(i)).toList() : null,
    );
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.data != null) 
      data['folders'] = this.data.map((v) => v.toJson()).toList();
    
    return data;
  


class Data 
  final String id;
  final String name;

  Data(this.id, this.name);

  factory Data.fromJson(Map<String, dynamic> json) 
    return Data(
      id: json['id'],
      name: json['name'],

    );
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    return data;
  

但是当我尝试解码 json 并通过 future 函数和这里是代码的小部件解析它时

class SecondScreen extends StatefulWidget 
  final YearsMain value;


  SecondScreen(Key key, this.value) : super(key: key);

  @override
  _SecondScreenState createState() => _SecondScreenState();


class _SecondScreenState extends State<SecondScreen> 
  Future<List<FolderList>>fellowSubject() async
    final jsonEndpoint =
        "http://msc-mu.com/api_verfication.php";

    final response = await http.post(jsonEndpoint,body: 
      'flag':'selectfellowsubjects',
      'parentsubject':widget.value.id
    );

    if (response.statusCode == 200) 
      List<dynamic> data = json.decode(response.body);
      return data;
     else
      throw Exception('We were not able to successfully download the Main Subjects.');

  
  @override
  void initState() 
    // TODO: implement initState
    super.initState();
    print(widget.value.id);
  
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      body:  Center(
        child: new FutureBuilder<List<FolderList>>(
          future: fellowSubject(),
          builder: (context, snapshot) 
            if (snapshot.hasData) 
              List<FolderList> fellow = snapshot.data;
              return ListViewFellow(fellow);
             else if (snapshot.hasError) 
              return Text('$snapshot.error');
            
            return CircularProgressIndicator();
          ,
        ),

      ),
    );
  

ListViewFellow

class ListViewFellow extends StatelessWidget 
  final List<FolderList> fellowSubject;
  ListViewFellow(this.fellowSubject);
  @override
  Widget build(BuildContext context) 
    return StaggeredGridView.countBuilder(
        crossAxisCount: 4,
        itemBuilder: (BuildContext context, int currentIndex) 
return createFellowItems(fellowSubject[currentIndex], context);
          ,
      staggeredTileBuilder: (int currentIndex) =>
      new StaggeredTile.count(2, currentIndex.isEven ? 2 : 2),
      mainAxisSpacing: 4.0,
      crossAxisSpacing: 1.0,
    );
  
  Widget createFellowItems(FolderList fellowData ,BuildContext context )
    return GestureDetector(
        child: Container(
          margin: EdgeInsets.all(15.0),
          decoration: BoxDecoration(
            color: Color(0xff131535),
            borderRadius: BorderRadius.circular(10.0),
          ),
          child: Center(
            child:
            (Text(fellowData.data[0].name))
          ),
        ),
        onTap: () 
          var route = new MaterialPageRoute(
            builder: (BuildContext context) => new DownloadPage(
              value: fellowData,
            ),
          );

          Navigator.of(context).push(route);
        );
  

我总是遇到上述错误我已经尝试了所有方法但对我没有任何效果,任何人都可以提供更好的解决方案吗?

Json 结构*


   "folders":[
      
         "id":2107,
         "name":"Labs ",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-09-30 03:00:39",
         "inarchive":0,
         "active":1
      ,
      
         "id":2108,
         "name":"L1",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-09-30 03:00:45",
         "inarchive":0,
         "active":1
      ,
      
         "id":2130,
         "name":"L2",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-02
02:39:11",
         "inarchive":0,
         "active":1
      ,
      
         "id":2175,
         "name":"L3",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-10-07 03:43:15",
         "inarchive":0,
         "active":1
      ,
      
         "id":2202,
         "name":"L4",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-09 02:08:10",
         "inarchive":0,
         "active":1
      ,
      
         "id":2227,
         "name":"\u0645\u0631\u0627\u062c\u0639\u0627\u062a
",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-10-11 05:15:43",
         "inarchive":0,
         "active":1
      ,
      
         "id":2242,
         "name":"L5",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-14 03:45:24",
         "inarchive":0,
         "active":1
      ,
      
         "id":2257,
         "name":"L6",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-16
06:23:41",
         "inarchive":0,
         "active":1
      ,
      
         "id":2269,
         "name":"L7",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-21 03:39:12",
         "inarchive":0,
         "active":1
      ,
      
         "id":2309,
         "name":"L8",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-23 05:14:48",
         "inarchive":0,
         "active":1
      ,
      
         "id":2310,
         "name":"L8",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-23
05:14:51",
         "inarchive":0,
         "active":1
      ,
      
         "id":2346,
         "name":"L9",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-28 04:22:24",
         "inarchive":0,
         "active":1
      ,
      
         "id":2370,
         "name":"L10",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-30 03:02:00",
         "inarchive":0,
         "active":1
      ,
      
         "id":2446,
         "name":"L11",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-12
06:28:13",
         "inarchive":0,
         "active":1
      ,
      
         "id":2462,
         "name":"\u062a\u0628\u064a\u0636\u0627\u062a",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-11-13 08:39:00",
         "inarchive":0,
         "active":1
      ,
      
         "id":2464,
         "name":"L12",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-13
08:40:55",
         "inarchive":0,
         "active":1
      ,
      
         "id":2488,
         "name":"L13",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-18 06:09:11",
         "inarchive":0,
         "active":1
      ,
      
         "id":2490,
         "name":"L14",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-18 06:38:59",
         "inarchive":0,
         "active":1
      ,
      
         "id":2491,
         "name":"L15",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-18
06:45:57",
         "inarchive":0,
         "active":1
      ,
      
         "id":2497,
         "name":"\u062a\u0644\u062e\u064a\u0635\u0627\u062a",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-11-19 04:24:49",
         "inarchive":0,
         "active":1
      ,
      
         "id":2509,
         "name":"L16",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-20
09:56:04",
         "inarchive":0,
         "active":1
      ,
      
         "id":2522,
         "name":"L17",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-25 06:38:27",
         "inarchive":0,
         "active":1
      ,
      
         "id":2542,
         "name":"L18",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-27 12:19:12",
         "inarchive":0,
         "active":1
      ,
      
         "id":2552,
         "name":"Protein
Lab",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-27 16:32:27",
         "inarchive":0,
         "active":1
      ,
      
         "id":2562,
         "name":"Chromatography ",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-30 15:24:24",
         "inarchive":0,
         "active":1
      ,
      
         "id":2576,
         "name":"L19",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-02
02:05:29",
         "inarchive":0,
         "active":1
      ,
      
         "id":2580,
         "name":"L20",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-02 08:23:48",
         "inarchive":0,
         "active":1
      ,
      
         "id":2592,
         "name":"21",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-03 06:27:24",
         "inarchive":0,
         "active":1
      ,
      
         "id":2600,
         "name":"L22",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-04
06:22:46",
         "inarchive":0,
         "active":1
      ,
      
         "id":2614,
         "name":"L23",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-04 10:03:00",
         "inarchive":0,
         "active":1
      ,
      
         "id":2640,
         "name":"L24",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-08 06:53:11",
         "inarchive":0,
         "active":1
      ,
      
         "id":2644,
         "name":"L25",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-09
05:33:07",
         "inarchive":0,
         "active":1
      ,
      
         "id":2649,
         "name":"L26",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-09 07:58:42",
         "inarchive":0,
         "active":1
      ,
      
         "id":2711,
         "name":"\u0623\u0633\u0626\u0644\u0629",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-19
05:18:11",
         "inarchive":0,
         "active":1
      
   ],
   "files":[
      
         "id":4542,
         "name":"Ch2-part1.m4a 27.52MB",
         "img":null,
         "uploader":"Aws",
         "url":"http:\/\/msc-mu.com\/..\/uploaded\/031020190Ch2-part1.m4a",
         "createdate":"2019-10-03 09:53:39",
         "approved":1,
         "active":1
      
   ]

【问题讨论】:

你能展示一下JSON 结构对于response.body 的样子吗? 你能指出代码吐出错误的那一行吗? json 是地图,而不是 List。映射包含键值对。 @Yadu 我真的不知道这就是为什么我在这里问而 android studio 没有显示任何东西 @Uni 那我该怎么办 【参考方案1】:

错误来自这里:List&lt;dynamic&gt; data = json.decode(response.body);.

JSON 的body 是一个 json 对象(映射)。 json.decode(response.body) 将返回 Map,但您尝试为其分配 List 从而引发该错误。

你应该这样做:

if (response.statusCode == 200) 
    var data = json.decode(response.body); 

    return [FolderList.fromJson(data)];
 else
    throw Exception('We were not able to successfully download the Main Subjects.');


【讨论】:

没有工作它说我不能将 用于 > 类型 那你应该return [FolderList.fromJson(data)]; iam rlly 抱歉,回复晚了,但你帮了我很多,现在表明 int 类型不是 string 类型的子类型 json 中的idint,但您的数据类接受String id。您应该 1) 将 String id 更改为 int id 或 2) 在您的 Data.fromJson 方法中使用 "$json['id']" 我可以和你不和吗?

以上是关于Flutter - type '_InternalLinkedHashMap<String, dynamic> 不是 List<dynamic> 类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章

Flutter - type '_InternalLinkedHashMap<String, dynamic> 不是 List<dynamic> 类型的子类型

执行 com.android.build.gradle.internal.tasks.Workers$ActionFacade 时发生故障 Flutter clean 后

node_modules/rxjs/internal/types.d.ts 中的角度错误

node_modules / rxjs / internal / types.d.ts中的Angular ERROR

“错误:在 react-redux 项目中安装节点包时找不到模块'internal/util/types'”

如何在不影响 Flutter 中的原始列表的情况下将列表执行深层复制到另一个列表