Exception: type 'int' is not a subtype of type 'String' flutter
Я пытаюсь получить данные из Cat API https://api.thecatapi.com/v1/breeds/ но получаю исключение Exception: type 'int' is not a subtype of type 'String' Никак не могу найти в каком месте я ошибся
CatList class
import 'package:flutter/material.dart';
import '../models/catData.dart';
class CatList extends StatelessWidget {
CatList({Key key, List<CatData> catList})
: _catList = catList,
super(key: key);
final List<CatData> _catList;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: _catList.length,
itemBuilder: (context, index) {
return Card(
elevation: 5,
margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
child: Container(
padding: EdgeInsets.all(15),
child: ListTile(
title: Text(
_catList[index].lifeSpan,
style: TextStyle(fontSize: 18, color: Colors.black),
),
)),
);
},
);
}
}
model class CatData
class CatData {
final String id;
final String name;
final String cfaUrl;
final String temperament;
final String origin;
final String countryCodes;
final String countryCode;
final String description;
final String lifeSpan;
final String childFriendly;
CatData(
this.id,
this.name,
this.cfaUrl,
this.temperament,
this.origin,
this.countryCodes,
this.countryCode,
this.description,
this.lifeSpan,
this.childFriendly);
CatData.fromJson(Map<String, dynamic> json)
: id = json['id'],
name = json['name'],
cfaUrl = json['cfa_url'],
temperament = json['temperament'],
origin = json['origin'],
countryCodes = json['country_codes'],
countryCode = json['country_code'],
description = json['description'],
lifeSpan = json['life_span'],
childFriendly = json['child_friendly'];
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'cfa_url': cfaUrl,
'temperament': temperament,
'origin': origin,
'country_codes': countryCodes,
'country_code': countryCode,
'description': description,
'life_span': lifeSpan,
'child_friendly': childFriendly,
};
}
main.dart
import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:flutter/foundation.dart';
import 'widgets/catList.dart';
import 'models/catData.dart';
void main() {
runApp(AnimalsListScreen());
}
Future<List<CatData>> fetchCats(http.Client client) async {
final response = await client.get('https://api.thecatapi.com/v1/breeds/');
return compute(parseCat, response.body);
}
List<CatData> parseCat(responseBody) {
final parsed = jsonDecode(responseBody) as List;
return parsed.map<CatData>((json) => CatData.fromJson(json)).toList();
}
class AnimalsListScreen extends StatefulWidget {
@override
_AnimalListScreenState createState() => _AnimalListScreenState();
}
class _AnimalListScreenState extends State<AnimalsListScreen> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Practice 3'),
),
body: FutureBuilder<List<CatData>>(
future: fetchCats(http.Client()),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? CatList(
catList: snapshot.data,
)
: Center(child: CircularProgressIndicator());
})),
);
}
}
Ответы (1 шт):
Автор решения: VAndrJ
→ Ссылка
Ну например в ответе:
"child_friendly":3 // число
А у Вас:
final String childFriendly; // строка
Максимально простой вариант - прогоните данные в app.quicktype.io и сравните результат со своим.