我已经使用Firebase的这种类型的调用有一段时间了,但是我尝试使用Map参数,我遇到了问题:这是一个模型:
import 'package:cloud_firestore/cloud_firestore.dart';
class Game {
final Map<String, String> name;
final String id;
Game({
required this.name,
required this.id,
});
Map<String, dynamic> toMap() {
return {
'Name': name,
'id': id,
};
}
factory Game.fromDocument(DocumentSnapshot documentSnapshot) {
return Game(
name: documentSnapshot['Name'],
id: documentSnapshot['id'],
);
}
}
但是最新的函数对Map不正确。我如何更正它,以便我能够使用此提供程序并在我的页面中调用它:
class GamesStateNotifier extends StateNotifier<List<Game>> {
final FirebaseFirestore firebaseFirestore;
final String pathGame;
StreamSubscription? _streamSubscription;
GamesStateNotifier({
required this.firebaseFirestore,
required this.pathGame,
}) : super([]);
Future<void> init() async {
_streamSubscription =
firebaseFirestore.collection(pathGame).snapshots().listen((event) {
state = event.docs.map((e) => Game.fromDocument(e)).toList();
});
}
@override
void dispose() {
if (_streamSubscription != null) {
_streamSubscription!.cancel();
}
super.dispose();
}
}
final gamesStateNotfierProvider =
StateNotifierProvider<GamesStateNotifier, List<Game>>((ref) {
final firestore = ref.watch(InstanteGames);
final path1 = ref.watch(PathGames);
return GamesStateNotifier(
firebaseFirestore: firestore,
pathGame: path1,
)..init();
});
调入页面:
final gamesWatch = ref.watch(gamesStateNotfierProvider);
1条答案
按热度按时间bd1hkmkf1#
我在.fromDocument函数上尝试了这个方法,但结果仍然是相同的错误:
打印出来的数据是这样的:
我不知道问题出在哪里,因为收到的数据匹配。