flutter 我怎么能从我的壁炉中取出阵列来飘动呢?

wnavrhmk  于 2023-05-23  发布在  Flutter
关注(0)|答案(1)|浏览(164)

我有一个简单的应用程序,我想获取我所有的数据并显示它,但我只能获取简单的字符串(名称,id和大小),但每当我试图获取数组它的错误:(有人能给予一个简单而快速的例子如何获取字符串数组到我的Flutter应用程序?

class _PokedexHomeState extends State<PokedexHome> {
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(
          leading: const Icon(MdiIcons.pokeball),
          centerTitle: true,
          title: const Center(child: Text('Pokédex')),
          backgroundColor: Colors.red),
      body: StreamBuilder(
        stream: readPokemon(),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return const Text('Couldn\u0027t find any Pokémon :( ');
          } else if (snapshot.hasData) {
            final users = snapshot.data!;
            return GridView.count(
              //amount of cards next to eachother
              crossAxisCount: 2,
              childAspectRatio: (1 / .5),
              children: users.map((e) => buildPokemon(e, context)).toList(),
            );
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }
}

class Pokemon {
  String id;
  final String name;
  final String size;
  final List<dynamic> elements;

  Pokemon(
      {this.id = '',
      required this.name,
      required this.size,
      required this.elements});

  Map<String, dynamic> toJson() =>
      {'id': id, 'name': name, 'size': size, 'elements': elements};

  static Pokemon fromJson(Map<String, dynamic> json) => Pokemon(
      id: json['id'],
      name: json['name'],
      size: json['size'],
      elements: json['elements'] as List<dynamic>);
}
ru9i0ody

ru9i0ody1#

试着改变

json['elements'] as List<dynamic>

List.from(json['elements'])

相关问题