如何将数据从Firestore检索到我的列表中

问题描述 投票:0回答:2
class Profile {

  final List<String> photos;
  final String name;
  ......

  Profile({
    this.photos,
    this.name,
    ......
  });

}

final List<Profile> demoProfiles = [
  Profile (
    photos: [
      "https:...",

    ],
    name: "Fatih",
    age: 22,
    distance: 4,
    education: "Hacettepe University"
  ),
  Profile (
    photos: [
       "https:...",
       "https:...",

    ],
    name: "Elysium",
    age: 23,
    distance: 2,
    bio: "Test bio"
  )
];

如何从Firestore检索数据,然后更新List demoProfiles并在main_controller.dart中使用它?我真的很陌生,有人可以帮助我吗?谢谢。

final MatchEngine matchEngine = MatchEngine (
    matches:demoProfiles.map((Profile profile) => Match(profile: profile)).toList()
  );

enter image description here

enter image description here

enter image description here

enter image description here

firebase flutter dart google-cloud-firestore future
2个回答
0
投票

您可以像下面这样尝试吗?

Future<Profile> fetchData() async {...}

0
投票

您可以这样做

List<Profile> demoProfiles = [];

@override
void initState() {
  super.initState();
  loadDataMethod();
}

void loadDataMethod() async {
  demoProfiles = await fetchData();
  setState(() {});
}


Future<List<Profile>> fetchData() async {
  List<Profile> list;
  // Fetch Data Logic
  return list;
}

OR like this

List<Profile> demoProfiles = fetchData() as List<Profile>;
© www.soinside.com 2019 - 2024. All rights reserved.