【问题标题】:Error : type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'错误:类型 '_InternalLinkedHashMap<String, dynamic>' 不是类型 'List<dynamic>' 的子类型
【发布时间】:2021-04-07 00:00:27
【问题描述】:

我是 Flutter 的新手,我尝试阅读 this json file,但出现错误,'like type List dynamic is not a subtype of type 'List.'

请帮助我学习一个有用的教程并告诉我如何修复此代码。

这是我的代码: resto_list_page.dart

import 'package:flutter/material.dart';
import 'resto.dart';

class RestoListPage extends StatelessWidget {
  static const routeName = '/resto_list';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Restaurant App'),
      ),
      body: FutureBuilder<dynamic>(
        future: DefaultAssetBundle.of(context)
            .loadString('assets/local_restaurant.json'),
        builder: (context, snapshot) {
          final List<Resto> restos = parseArticles(snapshot.data);
          return ListView.builder(
            itemCount: restos.length,
            itemBuilder: (context, index) {
              return _buildArticleItem(context, restos[index]);
            },
          );
        },
      ),
    );
  }
}

Widget _buildArticleItem(BuildContext context, Resto resto) {
  return ListTile(
    contentPadding:
        const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
    leading: Image.network(
      resto.pictureId,
      width: 100,
    ),
    title: Text(resto.name),
    subtitle: Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Icon(Icons.place),
            Text(resto.city)
          ],
        ),
        Row(
          children: <Widget>[
            Icon(Icons.star),
            Text('$resto.rating')
          ],
        ),
      ],
    ),
  );
}

这是另一个文件代码。 resto.dart

import 'dart:convert';

class Resto {
  String id;
  String name;
  String description;
  String pictureId;
  String city;
  double rating;
  Map<String, String> foods;
  Map<String, String> drinks;

  Resto(
      {this.id,
      this.name,
      this.description,
      this.pictureId,
      this.city,
      this.rating,
      this.foods,
      this.drinks});

  Resto.fromJson(Map<String, dynamic> restos) {
    id = restos['id'];
    name = restos['name'];
    description = restos['description'];
    pictureId = restos['pictureId'];
    city = restos['city'];
    rating = restos['rating'];
    foods = restos['menus']['foods'];
    drinks = restos['menus']['drinks'];
  }
}

List<Resto> parseArticles(String json) {
  if (json == null) {
    return [];
  }
  
  final List parsed = jsonDecode(json);
  return parsed.map((json) => Resto.fromJson(json)).toList();
}

【问题讨论】:

  • 请添加您的 json 文件。此错误表示您尝试解析为列表的 json 文件不是列表,而是地图。

标签: json api rest flutter


【解决方案1】:

parsed 必须声明为List&lt;dynamic&gt;

final List<dynamic> parsed = jsonDecode(json);

所以完整的代码变成了:

List<Resto> parseArticles(String json) {
  if (json == null) {
    return [];
  }
  
  final List<dynamic> parsed = jsonDecode(json);
  return parsed.map((json) => Resto.fromJson(json)).toList();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-22
    • 2020-07-03
    • 2021-01-30
    • 2019-04-03
    • 2021-01-13
    • 2022-11-11
    • 2022-07-21
    • 2019-01-26
    相关资源
    最近更新 更多