skip to Main Content

I created a simple firebase and would like to show my List of strings in flutter but it gives a snapshot error while the normal Strings (if I remove the List, the name and size) display normally. How can i display the List?

enter image description here

    class Pokemon {
  String id;
  final String name;
  final String size;
  final List<String> 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']
      );
}

Widget buildPokemon(Pokemon pokemon, BuildContext context) => (InkWell(
onTap: () {
  Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => PokemonDetailPage(
            pokemon: pokemon,
          )));
},
child: Card(
  elevation: 10.0,
  child: Column(
    children: [
      ListTile(
          title: Text(pokemon.name),
          subtitle: Text(pokemon.size + pokemon.elements[0]),
          trailing: IconButton(
              onPressed: () {
                QuickAlert.show(
                    context: context,
                    type: QuickAlertType.confirm,
                    title: "Sure you want to remove ${pokemon.name}?",
                    onConfirmBtnTap: () {
                      DocumentReference documentReference =
                          FirebaseFirestore.instance
                              .collection('Pokemon')
                              .doc(pokemon.id);
                      documentReference.delete();
                      Navigator.pop(context);
                    });
              },
              icon: const Icon(
                Icons.delete,
                color: Colors.red,
              ))),
    ],
  ),
)));

2

Answers


  1. elements is a List of Strings. Have you tried casting to List<String>?

    If that doesn’t work, try List<dynamic>

      static Pokemon fromJson(Map<String, dynamic> json) => Pokemon(
            id: json['id'],
            name: json['name'],
            size: json['size'],
            elements: json['elements'] as List<String> //change this line
          );
    
    Login or Signup to reply.
  2. This is how I was able to get a String[] from firestore in my flutter application working.

    factory RecipeBookModel.fromFirestore(
     DocumentSnapshot<Map<String, dynamic>> snapshot,
     SnapshotOptions? options,
    ) {
     final data = snapshot.data();
     return RecipeBookModel(
      id: data?['iid'],
      name: data?['name'],
      category: data?['category'],
      recipes: data?['recipes'] is Iterable ? List<String>.from(data?['recipes']) : null, // THIS LINE
      createdBy: data?['createdBy'],
      likes: data?['likes'],
     );
    }
    

    For Example in your example:

    ...
    elements: json['elmements'] is Iterable ? List<String>.from(json['elements']) : null
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search