Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

json - Error : type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'

I am newbie to Flutter, I try to read this json file but got error 'like type List dynamic is not a subtype of type 'List."

Please help me to follow a helpful tutorial and tell me how to fix this code.

Here is my code: 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')
          ],
        ),
      ],
    ),
  );
}

And here is another file code. 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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

parsed has to be declared as a List<dynamic>:

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

So the full code becomes:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...