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();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…