jeudi 1 avril 2021

ListView with Firebase data

since I decided to create a list of my Firestore-data my project became a whole horror trip. What I'm trying to do is following:

I want to import the data from my firestore database and include it into my list view. The following extract of my code throws no error but instead it shows the message when structs is empty. So my only idea is that the import of the firebase data or something to add this to my ListView didn't worked.

As I said that only an extract of my code but I think the error has to be somewhere inside here:

class __HomeStateState extends State<_HomeState> {
  final firestoreInstance = FirebaseFirestore.instance;

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<StructureProvider>(context);
    final structs = provider.structs;
    return structs.isEmpty
        ? Center(
            child: Text("Keine Einträge vorhanden"),
          )
        : Scaffold(
            body: StreamBuilder<List<Structure>>(
                stream: FirebaseApi.readStructs(),
                builder: (context, snapshot) {
                  final structs = snapshot.data;
                  final provider = Provider.of<StructureProvider>(context);
                  provider.setStructs(structs);

                  return ListView.separated(
                      padding: EdgeInsets.all(20),
                      physics: BouncingScrollPhysics(),
                      separatorBuilder: (context, index) => Container(
                            height: 10,
                          ),
                      itemCount: structs.length,
                      itemBuilder: (context, index) {
                        final struct = structs[index];
                        return StructureWidget(struct: struct);
                      });
                }));
  }
}

class Structure {
  DateTime createdTime;
  String vorname;
  String nachname;
  String geburtstag;
  String adresse;
  String telefon;
  int id;
  bool isDone;

  Structure(
      {@required this.createdTime,
      @required this.nachname,
      this.vorname = "",
      this.geburtstag = "",
      this.adresse = "",
      this.telefon = "",
      this.id,
      this.isDone});

  static Structure fromJson(Map<String, dynamic> json) => Structure(
        createdTime: Utils.toDateTime(json['createdTime']),
        vorname: json['vorname'],
        nachname: json['nachname'],
        geburtstag: json['geburtstag'],
        adresse: json['adresse'],
        telefon: json['telefon'],
        id: json['id'],
        isDone: json['isDone'],
      );
}


class StructureProvider extends ChangeNotifier {
  List<Structure> _struct = [];

  List<Structure> get structs => _struct.toList();

  void setStructs(List<Structure> structs) =>
      WidgetsBinding.instance.addPostFrameCallback((_) {
        _struct = structs;
        notifyListeners();
      });
}

class FirebaseApi {
  static Stream<List<Structure>> readStructs() => FirebaseFirestore.instance
      .collection("users")
      .snapshots()
      .transform(Utils.transformer(Structure.fromJson));
}

Thanks for any kind of help :)




Aucun commentaire:

Enregistrer un commentaire