我正在尝试制作一个口袋妖怪应用程序。但我收到此错误:
“getter 'pokemon' 被调用为 null。
接收者:空
尝试调用:pokemon”
I am putting error page to here.
我使用这个 API url 来获取数据“https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json”
我的口袋妖怪课程在这里:**
class Pokedex {
List<Pokemon> pokemon;
Pokedex({
this.pokemon});
Pokedex.fromJson(dynamic json) {
if (json["pokemon"] != null) {
pokemon = [];
json["pokemon"].forEach((v) {
pokemon.add(Pokemon.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
var map = <String, dynamic>{};
if (pokemon != null) {
map["pokemon"] = pokemon.map((v) => v.toJson()).toList();
}
return map;
}
}
class Pokemon {
int id;
String num;
String name;
String img;
List<String> type;
String height;
String weight;
String candy;
int candyCount;
String egg;
double spawnChance;
int avgSpawns;
String spawnTime;
List<double> multipliers;
List<String> weaknesses;
List<Next_evolution> nextEvolution;
Pokemon({
this.id,
this.num,
this.name,
this.img,
this.type,
this.height,
this.weight,
this.candy,
this.candyCount,
this.egg,
this.spawnChance,
this.avgSpawns,
this.spawnTime,
this.multipliers,
this.weaknesses,
this.nextEvolution});
Pokemon.fromJson(dynamic json) {
id = json["id"];
num = json["num"];
name = json["name"];
img = json["img"];
type = json["type"] != null ? json["type"].cast<String>() : [];
height = json["height"];
weight = json["weight"];
candy = json["candy"];
candyCount = json["candy_count"];
egg = json["egg"];
spawnChance = json["spawn_chance"];
avgSpawns = json["avg_spawns"];
spawnTime = json["spawn_time"];
multipliers = json["multipliers"] != null ? json["multipliers"].cast<double>() : [];
weaknesses = json["weaknesses"] != null ? json["weaknesses"].cast<String>() : [];
if (json["next_evolution"] != null) {
nextEvolution = [];
json["next_evolution"].forEach((v) {
nextEvolution.add(Next_evolution.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
var map = <String, dynamic>{};
map["id"] = id;
map["num"] = num;
map["name"] = name;
map["img"] = img;
map["type"] = type;
map["height"] = height;
map["weight"] = weight;
map["candy"] = candy;
map["candy_count"] = candyCount;
map["egg"] = egg;
map["spawn_chance"] = spawnChance;
map["avg_spawns"] = avgSpawns;
map["spawn_time"] = spawnTime;
map["multipliers"] = multipliers;
map["weaknesses"] = weaknesses;
if (nextEvolution != null) {
map["next_evolution"] = nextEvolution.map((v) => v.toJson()).toList();
}
return map;
}
}
class Next_evolution {
String num;
String name;
Next_evolution({
this.num,
this.name});
Next_evolution.fromJson(dynamic json) {
num = json["num"];
name = json["name"];
}
Map<String, dynamic> toJson() {
var map = <String, dynamic>{};
map["num"] = num;
map["name"] = name;
return map;
}
}
<块引用>
这是我的 main.dart 文件:
import 'package:flutter/material.dart';
import 'package:pokemon_api/pokemon_list.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: PokemonList(),
);
}
}
<块引用>
这是我的 pokemon_list.dart 文件:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'model/pokedex.dart';
class PokemonList extends StatefulWidget {
const PokemonList({Key key}) : super(key: key);
@override
_PokemonListState createState() => _PokemonListState();
}
class _PokemonListState extends State<PokemonList> {
String url =
"https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json";
Pokedex pokedex;
var response;
Future<Pokedex> PokemonlariGetir() async {
response= await http.get(Uri.parse(url)).then((value){
var decodedJson = json.decode(response.body);
pokedex = Pokedex.fromJson(decodedJson);
});
debugPrint(pokedex.toString());
return pokedex;
}
@override
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Pokemon"),
),
body: FutureBuilder(
future: PokemonlariGetir(),
builder: (context, AsyncSnapshot<Pokedex> gelenPokedex) {
if (gelenPokedex.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
} else if (gelenPokedex.connectionState==ConnectionState.done) {
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemBuilder: (context,index){
return Text(gelenPokedex.data.pokemon[index].name);
});
// return GridView.count(crossAxisCount: 2,children:gelenPokedex.data.pokemon.map((poke){
// return Text(poke.name);
// }).toList(),);
}else{
return Center(child: CircularProgressIndicator(),);
}
}),
);
}
}